【发布时间】: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