javascript - 消息 "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout"

标签 javascript automated-tests jestjs puppeteer

我正在使用 Puppeteer 和 Jest 运行一些前端测试。

我的测试如下所示:

describe("Profile Tab Exists and Clickable: /settings/user", () => {
    test(`Assert that you can click the profile tab`, async () => {
      await page.waitForSelector(PROFILE.TAB);
      await page.click(PROFILE.TAB);
    }, 30000);
});

有时,当我运行测试时,一切都按预期进行。其他时候,我会收到错误消息:

Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.

     at node_modules/jest-jasmine2/build/queue_runner.js:68:21 <br/>
     at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)

这很奇怪,因为:

  1. 我指定超时为30000

  2. 我是否收到此错误似乎是随机的

为什么会这样?

最佳答案

您在此处指定的超时时间需要短于默认超时时间。

默认超时为 5000,在 jest 的情况下,默认框架为 jasmine。您可以通过添加

来指定测试内部的超时
jest.setTimeout(30000);

但这将特定于测试。或者您可以为框架设置配置文件。

Configuring Jest

// jest.config.js
module.exports = {
  // setupTestFrameworkScriptFile has been deprecated in
  // favor of setupFilesAfterEnv in jest 24
  setupFilesAfterEnv: ['./jest.setup.js']
}

// jest.setup.js
jest.setTimeout(30000)

另请参阅这些主题:

setTimeout per test #5055

Make jasmine.DEFAULT_TIMEOUT_INTERVAL configurable #652

附注:拼写错误的 setupFilesAfterEnv(即 setupFileAfterEnv)也会引发同样的错误。

关于javascript - 消息 "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49603939/

相关文章:

reactjs - 如何在 Jest 中使用 async/await 对 Promise catch() 方法行为进行单元测试?

javascript - 如何在 mixpanel 别名成功时触发回调?

javascript - AngularJS ng-click 不会从 anchor 单击触发

javascript - 什么是 MeteorJS 中的 stub 方法?

javascript - 检查复选框是否被选中总是返回 true

unit-testing - 在命令行上运行 MSTest 时测试失败但在 VS2012 中通过

reactjs - 作为子组件的浅测试函数

reactjs - 使用 jest 进行 React 测试不支持 webpack 别名中的 moduleNameMapper

javascript - 单击提交按钮时文本框未清除

selenium - 如何使用 Selenium WebDriver(Java) 和 TestNG 按特定顺序运行测试类?