Learn

Now that we have a module set up with our mocked function it’s time to use it within our Jest test for findRecipe(). Remember, we want to replace the actual apiRequest() function with the mocked one we created!

Let’s take a look at the steps to use a function that we’ve created in our __mocks__/ directory in our tests.

  1. First, in the test file, we import the real function. This allows the test to work as it would otherwise if no mock existed.

  2. Then use the jest.mock() function provided by Jest to override the value with the mocked one defined in the __mocks__/ folder. jest.mock() accepts a string as an argument that should match the filepath to the file being mocked.

Note: This second step is only required when mocking local modules. When mocking modules installed into the node_modules directory, the module will automatically be mocked when it is imported in step 1.

// import the actual module import apiRequest from './api-request.js'; // then tell Jest to use the mocked version! jest.mock('./api-request.js');

Following these steps will cause any apiRequest() function calls made in the Jest tests to use the mocked version instead.

We can also further manipulate how specific methods in the mocked module behave by using special methods attached to functions mocked with jest.fn(). One of these methods is mockFunction.mockResolvedValueOnce(), which accepts a value that the next call to mockFunction() will resolve to.

// file: __tests__/recipes.js import { findRecipe } from './recipes.js'; // import the actual module import apiRequest from './api-request.js'; // then tell Jest to use the mocked version! jest.mock('./api-request.js'); test("get the full recipe for a dish", async () => { // arrange const dish = "Pesto"; const expectedValue = { "Magical Deliciousness": "3 cups" }; // set the resolved value for the next call to apiRequest const mockResponse = { status: "mock", data: { "Magical Deliciousness": "3 cups" } } apiRequest.mockResolvedValueOnce(mockResponse); // act const actualRecipe = await findRecipe(dish); // assertion expect(actualRecipe).toEqual(expectedValue); });

In the example above, the .mockResolvedValueOnce() method is used to determine what the next call to the apiRequest() function will resolve to. This method may be called any number of times in a test and may be useful if you’d like to finely control what each call to your mocked function resolves to.

There are many other methods available for controlling mocked functions. To see a full list of these methods, check out the Jest documentation.

Instructions

1.

In the last exercise, you mocked the httpRequest() function in the __mocks__/ folder. Now, back in the language_spoken.test.js file we can use it!

At the top of the language_spoken.test.js file, let’s begin by importing the httpRequest function from the '../utils/http-request.js' file. You should use the default import syntax:

import function from '/path/to/module.js'

Then, notify Jest that we want the file to be mocked!

2.

Now that we are using the mocked version of the httpRequest() function, we can control what value it resolves to so we can test countryListLookup() without using a real API.

In the second test() in language_spoken.test.js, the variable resolvedValue has been defined for you. Below, a call to countryListLookup() is made which is the function that uses httpRequest()!

Before the call to countryListLookup(), use the .mockResolvedValueOnce() to mock the next resolved value of httpRequest() to be resolvedValue.

3.

If you’ve completed the previous steps properly the expect() assertion made at the bottom of the second test() should now pass.

Run the test command in the terminal and you should see two passing tests. Also notice in the coverage report that the httpRequest() file is no longer being covered by our tests!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?