Timeout - Async call back not invoked when using jest.useFakeTimers('modern') and jest.setSystemTime()

Viewed 1675

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!

1 Answers

I had the same problem. After doing some research I found this "solution." I don't care for going into code in node_modules and manually changing a line. Nonetheless, it does fix the problem in a fairly simple manner. For me this will suffice until they finally release the fix. Wanted to post this link for the benefit of others who, like me, find this question first in their google search.

https://github.com/facebook/jest/issues/10221#issuecomment-654687396

Related