Table of contents
1.
Introduction 
2.
Mock Function
3.
Mock Implementation
4.
FAQs-
5.
Key takeaways-
Last Updated: Mar 27, 2024

Mock Implementation

Author Riya
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Mocking is a technique in unit testing in which the external dependencies of the code being tested are replaced by dummy implementations so that the code to be tested can be controlled and test runs can be more quick and reliable. In simple words, Mocking or Mock implementation is creating a dummy version of an external or internal service on which the code to be tested depends.

Mocking in Jest is generally replacing dependencies with mock functions. There are different ways for mock implementation in Jest, but before moving to those, let’s first discuss what mock functions are.

Mock Function

Mock functions are used for mock implementation. These functions are also called ‘spies’ because they can be used to spy on the behaviour of a function that is called by the external code instead of just testing. Mock functions provide a way to replace the implementation details of external dependencies and allow us to produce unit tests that are focused, reproducible, and independent of external factors.

There are two different ways to mock functions. First, create a mock function that will be used in test code, and the second is to write a manual mock to override a manual dependency.

Mock Implementation

In this section, we will discuss two ways of mock implementation.

  1. Mock a function with jest.fn
    This is the most basic way of mocking in which the original function is reassigned to a mock function. Then the mock function will be called at all the places of the reassigned function.
    “jest.fn(implementation)” or “jest.fn().mockImplementation(implementation)” accepts a function and uses it as the mock implementation.
     
  2. Mock a function with jest.spyOn
    The method jest.spyOn can be used to mock either the whole module or its individual functions. It is used in the cases where we need to spy on a method but keep the original implementation and in the cases where we need to mock the implementation but restore the original.
    “jest.spyOn(object, methodName)” creates a mock function as jest.fn but also tracks calls to object[methodName].

FAQs-

  1. When to use Mock Functions?

 Ans: We can use Mock Functions in two conditions. First, when we want to replace the      return value for a specific function. Second, when we want to check if our test subject is executing a function in a certain way

Key takeaways-

This article discussed about  “Mock Implementation” in detail. If you think this article helped to enhance your understanding about mocking and mock implementation, please upvote and share it with your friends.
To be more confident in data structures and algorithms, try out our DS and Algo Course.

Until then, All the best for your future endeavours, and Keep Coding.

Live masterclass