|
| 1 | +--- |
| 2 | +id: bypassing-module-mocks |
| 3 | +title: Bypassing module mocks |
| 4 | +--- |
| 5 | + |
| 6 | +Jest allows you to mock out whole modules in your tests, which can be useful for testing if your code is calling functions from that module correctly. However, sometimes you may want to use parts of a mocked module in your _test file_, in which case you want to access the original implementation, rather than a mocked version. |
| 7 | + |
| 8 | +Consider writing a test case for this `createUser` function: |
| 9 | + |
| 10 | +```javascript title="createUser.js" |
| 11 | +import fetch from 'node-fetch'; |
| 12 | + |
| 13 | +export const createUser = async () => { |
| 14 | + const response = await fetch('https://website.com/users', {method: 'POST'}); |
| 15 | + const userId = await response.text(); |
| 16 | + return userId; |
| 17 | +}; |
| 18 | +``` |
| 19 | + |
| 20 | +Your test will want to mock the `fetch` function so that we can be sure that it gets called without actually making the network request. However, you'll also need to mock the return value of `fetch` with a `Response` (wrapped in a `Promise`), as our function uses it to grab the created user's ID. So you might initially try writing a test like this: |
| 21 | + |
| 22 | +```javascript |
| 23 | +jest.mock('node-fetch'); |
| 24 | + |
| 25 | +import fetch, {Response} from 'node-fetch'; |
| 26 | +import {createUser} from './createUser'; |
| 27 | + |
| 28 | +test('createUser calls fetch with the right args and returns the user id', async () => { |
| 29 | + fetch.mockReturnValue(Promise.resolve(new Response('4'))); |
| 30 | + |
| 31 | + const userId = await createUser(); |
| 32 | + |
| 33 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 34 | + expect(fetch).toHaveBeenCalledWith('https://website.com/users', { |
| 35 | + method: 'POST', |
| 36 | + }); |
| 37 | + expect(userId).toBe('4'); |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +However, if you ran that test you would find that the `createUser` function would fail, throwing the error: `TypeError: response.text is not a function`. This is because the `Response` class you've imported from `node-fetch` has been mocked (due to the `jest.mock` call at the top of the test file) so it no longer behaves the way it should. |
| 42 | + |
| 43 | +To get around problems like this, Jest provides the `jest.requireActual` helper. To make the above test work, make the following change to the imports in the test file: |
| 44 | + |
| 45 | +```javascript |
| 46 | +// BEFORE |
| 47 | +jest.mock('node-fetch'); |
| 48 | +import fetch, {Response} from 'node-fetch'; |
| 49 | +``` |
| 50 | + |
| 51 | +```javascript |
| 52 | +// AFTER |
| 53 | +jest.mock('node-fetch'); |
| 54 | +import fetch from 'node-fetch'; |
| 55 | +const {Response} = jest.requireActual('node-fetch'); |
| 56 | +``` |
| 57 | + |
| 58 | +This allows your test file to import the actual `Response` object from `node-fetch`, rather than a mocked version. This means the test will now pass correctly. |
0 commit comments