I am trying to mock today's date using jests timing functions.
Previously, I was mocking out dayjs with the following mock:
jest.mock('dayjs', () => {
const mockDayjs = (date = 'Fri Sep 25 2020'): dayjs.Dayjs => jest.requireActual('dayjs')(date);
return mockDayjs;
});
It was suggested that I change this to use the out of the box jest functions as follows:
beforeAll(() => {
jest.useFakeTimers('modern');
jest.setSystemTime(new Date('Fri Sep 25 2020'));
});
afterAll(() => {
jest.useRealTimers();
});
This worked for one unit test, but failed for another test with the following error:
● server: custom reports › post - success
: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:
The only main difference between the two tests is that the failing test is asynchronous. Wondering if I am missing something here. Thanks!