Home > Back-end >  Mock imported function in Jest
Mock imported function in Jest

Time:10-15

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, e.g. "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

CodePudding user response:

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');
});
  • Related