5 octubre, 2024

Applying MockReturnValue in Jest [Guía práctica]

If you’ve been working with Jest for JavaScript testing, you’ve probably seen the term mock function. In this post, we will delve into a specific aspect of mock functions: MockReturnValue in Jest. Additionally, we’ll explore MockReturnValueOnce to give your tests a little more flexibility.

MockReturnValue allows you to simulate the behavior of a function in Jest. That is, instead of allowing the function to run normally, you can fake or mock its behavior and control what it returns. This is especially useful when dealing with functions that have side effects, such as requests to an API, interactions with a database, or DOM manipulation.

What is a mock function?

To test effectively, we need to have control over the functions we are testing. This is where the mock functions or simulated functions.

A mock function is basically a function that records information about how it is called during code execution. It allows us to simulate specific behavior of the function, such as returning specific values, implementing custom behaviors, and generally giving us more granular control over how our function behaves in tests.

In Jest, mock functions are created using jest.fn() or jest.mock(). But how do we control the value that a mock function returns? This is where MockReturnValue in Jest and mockReturnValueOnce come into the picture.

For example, you could have a function called getUserName(), which typically makes a request to an API and returns a username. When writing tests, you don’t want to make a real request to the API, but you can use MockReturnValue to fake the API response.

Control return value with MockReturnValue in Jest

The MockReturnValue function in Jest is used to control what a mock function returns when called. Let’s see how it works:

const mockFn = jest.fn(); mockFn.mockReturnValue(‘Hello, !’); console.log(mockFn()); // Print: Hello, !

In this example, mockFn It is a mock function. We use MockReturnValue in Jest to make the function always return the string ‘Hello, !’. Now every time we call mockFn()we will receive that string.

With MockReturnValue in Jest, you can ensure that your tests are fast, consistent, and reliable. You can focus on testing the behavior of your own code, rather than relying on external services. You can also simulate different responses and behaviors to test all possible scenarios of your application.

Take it a step further with MockReturnValueOnce

Sometimes you may want your mock function return different values ​​in consecutive calls. This can be done with mockReturnValueOnce. This method, as the name suggests, causes the mock function to return a specific value only once, on the next call. After that, he will return to his normal behavior.

const mockFn = jest.fn(); mockFn.mockReturnValueOnce(‘Hello, !’); mockFn.mockReturnValue(‘Hello, World!’); console.log(mockFn()); // Print: Hello, ! console.log(mockFn()); // Print: Hello, World!

In this example, mockFn will return ‘Hello, !’ the first time it is called. On subsequent calls, it will return ‘Hello, World!’.

Reboot and restore with mockReset and mockRestore

If you want to clean the calls and instances of your mock functionyou can use mockReset(), but if what you need is to restore the original implementation of the function, mockRestore() is what you are looking for.

In summary, MockReturnValue in Jest and mockReturnValueOnce are powerful tools that allow us to control how our mock functions behave during testing. They allow us to design more detailed and accurate tests, which in turn helps us write more robust and reliable code.

Continue learning in our bootcamp

If you liked this walk through the world of MockReturnValue in Jest, you will love our Full Stack Web Development Bootcamp in . We will teach you theoretically and practically from the most basic to the most advanced web development and, most importantly, you will learn to think and solve problems like a real developer.

Remember that the technology industry is hungry for competent professionals and, upon completing this comprehensive, high-intensity training, you will be ready to succeed in the IT job market. Give your life a change, sign up for the bootcamp and immerse yourself in the fascinating world of web development!

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *