Mock your tests with Jest

For a better understanding of mocking the tests using Jest for Automation testers and developers.

Aruna Thennakoon
4 min readFeb 19, 2023

Mocking

Mocking is a powerful tool for testing JavaScript code, particularly when testing functions’ behavior. This is a technique to isolate a unit of code from its dependencies and test its behavior. In JavaScript, mocking makes it possible to test code that uses functions from external libraries or other parts of an application. By replacing a function with a mock, you can control its output and validate that it was called with the correct arguments.

Jest

Jest is an open-source JavaScript testing framework created by Facebook that is designed to be used for unit testing, and integration testing and can be used to test applications written in React, Angular, Vue, Node.js, and more. Jest mock functions, also known as “spies”, are powerful and versatile functions that can be used to create stubs, mocks, and spies in Jest.

The Jest’s mock function is a special type of function that records the calls it receives and the arguments it is called with. It can also be used to mock the return value of a function. This makes it possible to test functions that depend on other functions, without having to actually call the other functions.

Jest library has a few ways to mock a function.

1. jest.fn()

2. jest.spyOn()

3. jest.mock()

Mock a function using jest.fn()

just.fn() returns undefined if no implementation is given.

Then the function can be mocked with a return value when required.

This has a very rich API so a lot of properties and behaviors can be validated.

expect(mockFunction).toHaveBeenCalled();

expect(mockFunction).toHaveBeenCalledTimes();

expect(mockFunction).toHaveBeenNthCalledWith();

Mock a function using jest.spyOn()

The jest.spyOn() method is another way to create a mock function. Like jest.fn(), it creates a controlled mock. The key distinction is that jest.spyOn() calls the original implementation by default. It stores the original implementation in memory, so if it has been altered, jest.spyOn() allows us to restore the initial implementation using the mockRestore() method. This is not something that can be done using jest.fn().

This is the module that should be mocked. The module can be created somewhere else. eg. util.js.

Then this module can be mocked using jest.spyOn() and the test file will be looked like this.

Cannot use jest.fn() in the above example since jest.fn() returns undefined if there is no implementation. But jest.spyOn() calls the original implementation.

But still can implement a behavior using the ‘mockImplementation’ method.

Mock a module using jest.mock()

Jest.mock() is a powerful tool for mocking modules in unit tests. It allows you to create a mock module that is able to intercept and mock any function calls made to the module. This allows you to simulate the behavior of the module in a controlled manner and make sure that the tests are not affected by external changes.

Mocking the module can be done by mocking all the functions of the module using jest.fn() as well. As an example,

Mocking individual methods is not necessarily difficult, but it can be time-consuming. Depending on the size and complexity of the module under test, it may be more efficient to mock out the entire module instead of mocking each method individually. That is why the jest.mock() comes in.

--

--

Aruna Thennakoon
0 Followers

I am a Test Automation Consultant with more than 12 years of experience in the software testing industry.