草庐IT

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

coder 2023-07-03 原文

我正在使用 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/

有关javascript - 消息 "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout"的更多相关文章

随机推荐