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