reactjs - Jest 在测试运行完成后一秒钟没有退出

标签 reactjs react-native jestjs

运行测试用例时出现以下错误:

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/

相关文章:

javascript - 在函数react中传递默认参数值

javascript - 自定义组件不接受样式React Material ui

javascript - 意外 token 模块构建失败 : SyntaxError

android - 无法使用 npm install -g 安装 turtle-cli

typescript - 无法模拟 API 请求

reactjs - 当scrollTop为零时,在渲染后 react 滚动位置

reactjs - 如何以编程方式扩展 React Reach UI (@reach/menu-button) 菜单?

reactjs - react native 选项卡导航器延迟加载

javascript - Jest Enzyme 测试在 render 方法中返回 null 的 React 组件

jestjs - Jest 失败并出现 SyntaxError : Unexpected token {