运行测试用例时出现以下错误:
Jest did not exit one second after the test run has completed. This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with
--detectOpenHandles
to troubleshoot this issue.
我使用 --ForceExit
和 --detectOpenHandles
运行了测试用例。但是我没有解决这个错误。
最佳答案
test("Fetch Token ", async done => {
await getToken("DEV", "myApp", token => {
console.log("AuthToken: ", authToken);
expect(authToken).toBeFalsy();
});
done();
});
2 可能的原因:
- 您的异步请求
getToken
使用了错误的变量,而不是authToken
,它是token
。
test("Fetch Token ", async done => {
await getToken("DEV", "myApp", token => {
console.log("AuthToken: ", token);
expect(token).toBeFalsy();
});
done();
});
- 您的异步请求响应时间过长,请尝试增加 jest 超时
jest.setTimeout(5000)
建议
当你使用 async-await 时,你不需要使用 done。
- 使用异步等待
test("Fetch Token ", async done => {
const authToken = await getToken("DEV", "myApp", token => {
return token;
});
console.log("AuthToken: ", authToken);
expect(authToken).toBeFalsy();
done();
});
- 没有异步等待
test("Fetch Token ", (done) => {
getToken("DEV", "myApp", token => {
console.log("AuthToken: ", token);
expect(token).toBeFalsy();
done();
});
});
关于reactjs - Jest 在测试运行完成后一秒钟没有退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54306511/