Unit testing 超时-使用jest.useFakeTimers(';modern';)和jest.setSystemTime()时未调用异步回调

Unit testing 超时-使用jest.useFakeTimers(';modern';)和jest.setSystemTime()时未调用异步回调,unit-testing,asynchronous,jestjs,timing,Unit Testing,Asynchronous,Jestjs,Timing,我试图用笑话来嘲弄今天的约会 之前,我用以下模拟来模拟dayjs: jest.mock('dayjs', () => { const mockDayjs = (date = 'Fri Sep 25 2020'): dayjs.Dayjs => jest.requireActual('dayjs')(date); return mockDayjs; }); 有人建议我将其更改为使用开箱即用的jest函数,如下所示: beforeAll(() => { jes

我试图用笑话来嘲弄今天的约会

之前,我用以下模拟来模拟
dayjs

jest.mock('dayjs', () => {
  const mockDayjs = (date = 'Fri Sep 25 2020'): dayjs.Dayjs => jest.requireActual('dayjs')(date);
  return mockDayjs;
});
有人建议我将其更改为使用开箱即用的jest函数,如下所示:

  beforeAll(() => {
    jest.useFakeTimers('modern');
    jest.setSystemTime(new Date('Fri Sep 25 2020'));
  });

  afterAll(() => {
    jest.useRealTimers();
  });

这在一个单元测试中有效,但在另一个测试中失败,出现以下错误:

● 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:

这两个测试之间唯一的主要区别是失败的测试是异步的。不知道我是否错过了什么。谢谢

我也有同样的问题。在做了一些研究之后,我发现了这个“解决方案”。我不喜欢进入
node\u模块中的代码,也不喜欢手动更改行。尽管如此,它还是以一种相当简单的方式解决了这个问题。对我来说,这就足够了,直到他们最终发布修复程序。想发布这个链接是为了其他人的利益,像我一样,在谷歌搜索中首先发现这个问题


“失败的测试是异步的吗”以及测试是什么样子的?该错误意味着
Jest
正在等待异步测试完成,但它从未完成……我猜您的测试函数正在使用
done
参数,但没有调用它