【问题标题】:Timeout - Async call back not invoked when using jest.useFakeTimers('modern') and jest.setSystemTime()超时 - 使用 jest.useFakeTimers('modern') 和 jest.setSystemTime() 时未调用异步回调
【发布时间】:2020-10-07 18:40:35
【问题描述】:

我正在尝试用笑话 timing functions 来模拟今天的日期。

以前,我使用以下模拟来模拟 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:

这两个测试之间唯一的主要区别是失败的测试是异步的。想知道我是否在这里遗漏了什么。谢谢!

【问题讨论】:

  • " 失败的测试是异步的" 以及测试是什么样子的?
  • 该错误意味着 Jest 正在等待您的异步测试完成,但它永远不会完成...我猜您的测试函数正在使用 done 参数但没有调用它

标签: unit-testing asynchronous jestjs timing


【解决方案1】:

我遇到了同样的问题。在做了一些研究之后,我发现了这个“解决方案”。我不在乎进入node_modules 中的代码并手动更改一行。尽管如此,它确实以相当简单的方式解决了问题。对我来说,这已经足够了,直到他们最终发布修复程序。想要发布此链接,以帮助像我一样首先在 Google 搜索中找到此问题的其他人。

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

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 2017-07-24
    • 2020-05-13
    • 2020-01-07
    • 2018-07-30
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多