上下文
我正在尝试使用 Jest (24.9( 编写一个测试用例,该用例具有异步回调函数,并且在运行时会成功。 我写的测试用例似乎应该可以工作,但它失败了,我不清楚为什么。
测试用例
这是测试用例。 它应该等待大约 100 毫秒,然后成功。
test("async test case", async () => {
await (new Promise((resolve) => {
setTimeout(() => {expect(true).toBe(true)}, 100);
}));
}, 10000);
错误信息
当我运行测试用例时,它失败并显示以下错误消息:
: Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.
Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Error:
at new Spec (C:Users<username>WebstormProjectsjest-async-testnode_modulesjest-jasmine2buildjasmineSpec.js:116:22)
at new Spec (C:Users<username>WebstormProjectsjest-async-testnode_modulesjest-jasmine2buildsetup_jest_globals.js:80:9)
at new JBPatchedSpec (C:Program FilesJetBrainsWebStorm 2019.2.1pluginsJavaScriptLanguagehelpersjest-intellijlibjest-intellij-jasmine-reporter.js:94:7)
at specFactory (C:Users<username>WebstormProjectsjest-async-testnode_modulesjest-jasmine2buildjasmineEnv.js:575:24)
at Env.it (C:Users<username>WebstormProjectsjest-async-testnode_modulesjest-jasmine2buildjasmineEnv.js:644:24) at Env.it (C:Users<username>WebstormProjectsjest-async-testnode_modulesjest-jasmine2buildjasmineAsyncInstall.js:132:23)
at it (C:Users<username>WebstormProjectsjest-async-testnode_modulesjest-jasmine2buildjasminejasmineLight.js:93:21)
at Object.<anonymous> (C:Users<username>WebstormProjectsjest-async-testasync.test.js:2:25)
...
问题
为什么此测试用例失败?
具有异步回调的普通测试用例的示例会是什么?
此测试用例中有几个回调。 错误消息指的是哪一个?
这是一个简单的错误。 正如@Emissary所指出的,应许永远不会得到解决。 测试用例的通过版本如下所示:
test("async test case", async () => {
await (new Promise((resolve) => {
setTimeout(() => {expect(true).toBe(true)}, 100);
resolve();
}));
}, 10000);