我在用木偶师和小丑做一些前端测试。

我的测试如下:

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 -在jest.setTimeout指定的5000毫秒超时时间内没有调用异步回调。 在node_modules/jest-jasmine2/build/queue_runner.js:68:21 <br/> 在超时。callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)

这很奇怪,因为:

我将超时时间指定为30000 我是否得到这个错误似乎是随机的

为什么会这样?


当前回答

此处指定的超时时间必须小于默认超时时间。

默认的超时时间是5000,框架默认为jasmine。您可以通过添加在测试中指定超时

jest.setTimeout(30000);

但这是针对测试的。或者,您可以为框架设置配置文件。

配置是

// 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)

请参见这些线程:

5055号考试第七次出局

茉莉花。DEFAULT_TIMEOUT_INTERVAL可配置#652

注:拼写错误的setupFilesAfterEnv(即setupFileAfterEnv)也会抛出相同的错误。

其他回答

此处指定的超时时间必须小于默认超时时间。

默认的超时时间是5000,框架默认为jasmine。您可以通过添加在测试中指定超时

jest.setTimeout(30000);

但这是针对测试的。或者,您可以为框架设置配置文件。

配置是

// 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)

请参见这些线程:

5055号考试第七次出局

茉莉花。DEFAULT_TIMEOUT_INTERVAL可配置#652

注:拼写错误的setupFilesAfterEnv(即setupFileAfterEnv)也会抛出相同的错误。

当它是async from test时,它应该调用async/await。

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

事实证明,如果您的expect断言是错误的,它有时会吐出超过超时的错误消息。

我能够通过在promise回调中放入console.log()语句来解决这个问题,并看到console.log()语句在jest输出中运行。一旦我修复了expect断言,超时错误就消失了,测试可以正常工作。

我花了很长时间才弄明白,希望这能帮助需要阅读这篇文章的人。

如果有人不解决这个问题,请使用上述方法。我通过用箭头函数包围async func来修正我的错误。如:

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);
});

当网络很慢或使用await进行许多网络调用时,就会出现超时问题。这些场景超过了缺省超时时间,即5000毫秒。要避免超时错误,只需增加支持超时的全局变量的超时。全局变量及其签名的列表可以在这里找到。

对于 是 24.9