April 16, 2024

Jest Mocking for Unit Testing

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Mocking is a technique used in software development to separate the unit under test from its dependencies, especially when doing unit tests. It’s crucial to test a unit of code’s specific functionality independently, as opposed to how it interacts with other components or systems, while designing unit tests. Developers can use “Mocks,” which are simulated copies of actual software components. Although they mimic the original versions behaviour, these fake versions don’t really run the original code. Rather than utilizing the actual external dependencies, they imitate the behaviour, enabling developers to test their code.

For example, consider a scenario of testing a function to add a record to the database, before adding the record it needs to pass the authentication. Authentication function returns true for authenticated user and false for unauthenticated user. You can use a mocked function for the authentication module that always returns a successful authentication response. This ensures that the focus of the test remains on the functionality of the database without being affected by the complexity of the authentication process.

Mocking of Modules and functions:

jest.mock() => Mock the entire Module.

jest.fn() => Mock the functions without mocking the entire module.

jest.spyOn() => Spy the function.

Before delving into each function, we need to understand the built-in function provided by Jest for mocking.

mockResolvedValueOnce

is used to simulate asynchronous functions that give back a Promise. To be more precise, it sets the resolved value of the Promise it returns and mocks a single function call. When testing asynchronous functions, this is helpful since it allows you to manage the resolved value of the Promise that the function returns for particular test cases.

mockRejectedValueOnce

is used to simulate asynchronous functions that return a Promise but ought to be refused. It sets the rejected value of the Promise it returns and mocks a single function call. This comes in handy for situations where you need to evaluate an asynchronous function’s ability to handle rejected Promises correctly.

mockReturnValueOnce

is used to simulate synchronous operations. It sets the return value and mocks a single call to the function or method. This is helpful in test scenarios where you need to manage a function or method’s return value for particular test cases.

Let us see each method of mocking one by one

Mocking using jest.mock():

In the below calculator.js there are three functions namely addition, subtraction, multiplication. Let us see how to mock it in the calculator.test.js file. Here we mock the calculator module entirely.

Mocking Using jest.fn():

Here we mock only two functions (addition and subtraction) present in the module.

Let us consider the same example (calculator.js) for understanding.

Mocking using jest.spyOn():

It is used to mock the function, in addition to that we can able to track our method being called without changing the implementation.

Let us consider the same example (calculator.js) for understanding.

Now we will see how to mock the function present in the different modules.

For example, consider the scenario of adding a record to the database. In actual flow first it will go to authentication (code for authentication is present in the another module) and after that it will go add a record. While testing the code for adding the record to the database there is no need to test the code for authentication. So that we can mock the authentication to return the success response and then we can test the code for our adding the record to database.

Consider the file Authentication.js, it has the function named verifyUser which is responsible for verifying the user who is authorised user. It returns a Boolean value true and false.

Consider the file addRecord which is responsible for adding the record.

Let us write mock test case for it:

CodeStax.Ai
Profile
April 16, 2024
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article

More articles

Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.