简体   繁体   中英

Mock imported function in Jest

I have a case:

test.js

import { today } from "utils/date";
import myFunction from "helpers/myFunction";

it('should work properly', () => {
   jest.mock('utils/date', () => ({
      ...(jest.requireActual('utils/date')),
      today: jest.fn(() => '01-01-2020'),
   }));

   console.log(today()); // still logs current date 14-10-2021, not the mocked date       

   expect(myFunction()).toEqual(today());
});

myFunction.js

import { today } from "utils/date";

export const myFunction = () => today();

today is a function that returns todays date. But for testing purposes I need that function to always return the same date , eg "01-01-2020" .

Note: as you can see that "today" function is used in the test as well as inside the tested (myFunction) function, so it has to return the same mocked value like everywhere in the app.

Thanks

jest.mock() is invoked in the test case functional scope. Module imports are hoisted (internally moved to the beginning of the current scope). The original today function is imported before jest.mock() mocks the utils/date module.

You can move the jest.mock() from test case functional scope to module scope. Jest will automatically hoist jest.mock calls to the top of the module (before any imports). So that when you import the today function, it is already being mocked.

See Using with ES module imports :

If you're using ES module imports then you'll normally be inclined to put your import statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist jest.mock calls to the top of the module (before any imports).

import { today } from 'utils/date';

jest.mock('utils/date', () => ({
  today: jest.fn(() => '01-01-2020'),
}));

it('should work properly', () => {
  expect(jest.isMockFunction(today)).toBeTruthy();
  expect(today()).toBe('01-01-2020');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM