How to Mock a Single Function from a Module with Jest

Mocking an entire module with Jest is easy. But what if you only want to mock some of the functions in a module?

Dunja Vesinger
JavaScript in Plain English

--

Photo by Thought Catalog on Unsplash

Jest is a great testing framework for JavaScript (or TypeScript). It offers a lot of functionalities and plenty of guides (with examples) on how to get them working.

Want to create a mock function and pass it in as a callback to the component you’re testing? Sure! You can even give it a custom mock implementation if you want.

Need to mock out an entire module or library? It only takes one line of code. And you can also provide custom mock implementations to those. You can even create mocks for specific methods of an object.

But, do you know that feeling when there are 50 different examples in the guide that seem to cover everything under the Sun except the use case you’re looking for? Ok, that might be a bit of a dramatic interpretation, but you get the gist.

What if you don’t want to mock out an entire module or library? What if you only wanted to mock one of its functions? Or a few selected ones?

It took me quite a while to put together the right Google query for that one. So here’s a short guide in case you ever end up in the same boat. (And yes, I managed to find an example for it in the official documentation as well, once I knew what to look for.)

Mocking a Single Function

There are three simple (and a bit confusing) steps of mocking only one function of a module and leaving the rest of it to its default implementation.

  1. Create a mock for the entire module.
  2. Tell that mock to use actual (non-mocked) implementations for everything.
  3. Mock the desired function of the mock.

A bit of a tongue twister, isn’t it? But the syntax is really simple and each of these steps only takes one line of code.

First, you create a mock of a module using the jest.mock function. The mock function takes a string that represents a path to the local module or a name of the library you’d like to mock, just like an import statement does. I’m going to refer to this as path in the example.

You then tell the mock to use real implementations of everything in the module using the jest.requireActual function. You should pass in the same string that represents a path or name that you passed in the mock function.

After you pass in the requirement for actual implementations, you can override the implementations of the functions you’d like to mock. You simply list function names and give it a value of jest.fn().

jest.fn() will create an empty implementation, but you can override it to take arguments and have a custom return value.

Here’s the three lines of code you need to write in your test to get it to work.

In newer versions of Jest, you might need to store the result of jest.requireActual in a variable and then use the spread operator like below.

And that’s all there is to it. Thank you for reading!

Resources

More content at plainenglish.io

--

--