在jest.setTimeout指定的5000ms超时内未调用异步回调

Aso*_*ool 152 javascript automated-tests jestjs puppeteer

我正在使用木偶戏和开玩笑来进行一些前端测试.

我的测试看起来如下:

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);
});
Run Code Online (Sandbox Code Playgroud)

有时,当我运行测试时,一切都按预期工作.其他时候,我收到一个错误:

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

      at node_modules/jest-jasmine2/build/queue_runner.js:68:21
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为:

  1. 我指定超时为30000

  2. 我是否得到这个错误似乎非常随机

任何人都可以猜到为什么会这样吗?

Tar*_*ani 165

因此,您在此处指定的超时需要短于默认超时.

默认超时是5000,默认jasmine情况下框架是jest.您可以通过添加来指定测试内的超时

jest.setTimeout(30000);
Run Code Online (Sandbox Code Playgroud)

但这将是测试的具体内容.或者您可以为框架设置配置文件.

https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string

// jest.config.js
module.exports = {
  setupTestFrameworkScriptFile: './jest.setup.js'
}

// jest.setup.js
jest.setTimeout(30000)
Run Code Online (Sandbox Code Playgroud)

也见这个帖子

https://github.com/facebook/jest/issues/5055

https://github.com/facebook/jest/issues/652

  • 由于这对我有所帮助,可能值得注意的是,已将setupTestFrameworkScriptFile替换为setupFilesAfterEnv,因此它变成了setupFilesAfterEnv:[“ ./jest.setup.js”]。 (13认同)
  • 感谢您回答我无法通过 Jest 文档轻松找到的问题。 (2认同)
  • 我还发现“jest.setTimeout(10000)”可以添加到边缘情况的单个测试中,因此整个配置不需要更改:) (2认同)

e-s*_*yut 59

这是一个相对较新的更新,但它更直接。如果您使用的是 Jest 24.9.0 或更高版本,您只需添加testTimeout到您的配置中:

// in jest.config.js
module.exports = {
  testTimeout: 30000
}
Run Code Online (Sandbox Code Playgroud)

  • 我希望这是公认的答案,比必须将此配置放在单独的文件中更简单。 (4认同)

sch*_*ode 43

它应该在测试异步时调用done函数.

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);
});
Run Code Online (Sandbox Code Playgroud)

  • 我们为什么要在异步函数中使用`done`?我们不是简单地返回Promise或undefined吗? (16认同)
  • 这与原始问题中的代码不同吗? (15认同)
  • 不,这不正确。您不需要调用 done() ,因为您正在等待您的承诺,或者您可以只返回 `page.click`。done() 至少在我的情况下主要用于测试回调。 (5认同)
  • 回调中存在参数(在本例中名为“done”)会导致 Jest 等待,直到调用该参数。即使不使用它,它的存在也很重要。 (5认同)
  • 没有`done();`,我在这里看不到任何答案。 (3认同)
  • 谢谢大家,我已经删除了不需要的`done` 回调。 (2认同)

ndp*_*ndp 41

随着Jest的发展,这个问题的答案已经改变。当前答案(2019年3月):

  1. 您可以通过在中添加第三个参数来覆盖任何单个测试的超时it。即。it('runs slow', () => {...}, 9999)

  2. 您可以使用更改默认设置jest.setTimeout。去做这个:

 // config
   "setupFilesAfterEnv": [  // NOT setupFiles
     "./src/jest/defaultTimeout.js"
   ],
Run Code Online (Sandbox Code Playgroud)

// File: src/jest/defaultTimeout.js
/* global jest */
jest.setTimeout(1000)
Run Code Online (Sandbox Code Playgroud)
  1. 就像其他人指出的那样,并且与之没有直接关系done,异步/等待方法不是必需的。

  • 这是更现代的版本 (4认同)

小智 23

对于 Jest 24.9+,我们只需要在命令行中添加--testTimeout

--testTimeout= 10000 // Timeout of 10 seconds
Run Code Online (Sandbox Code Playgroud)

默认超时值为 5000(5000 毫秒 - 5 秒)。这将适用于所有测试用例。

或者,如果您只想给特定函数超时,那么您可以在声明测试用例时使用此语法。

test(name, fn, timeout)
Run Code Online (Sandbox Code Playgroud)

例子

test('example', async () => {

}, 10000); // Timeout of 10 seconds (default is 5000 ms)
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 21

我想补充一点(这有点长的评论)即使3000我的测试超时仍然有时(随机)失败

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
Run Code Online (Sandbox Code Playgroud)

感谢@ Tarun的出色答案,我认为修复大量测试的最短路径是:

describe('puppeteer tests', () => {
  beforeEach(() => {
    jest.setTimeout(10000);
  });

  test('best jest test fest', async () => {
    // blah
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 您不需要在`beforeEach`中调用`jest.setTimeout()`,一次调用就足够了所有测试。 (9认同)

Zen*_*tzi 13

确保done();对回调进行调用,否则它不会简单地通过测试。

beforeAll((done /* call it or remove it*/) => {
  done(); // calling it
});
Run Code Online (Sandbox Code Playgroud)

适用于所有具有done()回调的其他函数。


use*_*237 9

2022 年 3 月 14 日,Jest 27.5 文档指出了一个新流程:

https://jestjs.io/docs/api#beforeallfn-timeout

传递第二个参数来测试超时前的毫秒数。作品!

test('adds 1 + 2 to equal 3', () => {
    expect(3).toBe(3);
},30000);
Run Code Online (Sandbox Code Playgroud)


ser*_*inc 8

另一个解决方案:在Jest 配置文件中设置超时,例如:

{ // ... other stuff here
    "testTimeout": 90000
}
Run Code Online (Sandbox Code Playgroud)


Mr.*_* 14 7

对于jest 24.9+,您还可以通过添加以下命令从命令行设置超时 --testTimeout

这是其文档的摘录

--testTimeout=<number>
Default timeout of a test in milliseconds. Default value: 5000.
Run Code Online (Sandbox Code Playgroud)


Jin*_*Yao 7

test接受一个timeout论点。请参阅https://jestjs.io/docs/api#testname-fn-timeout。这是一个示例:

async function wait(millis) {
  console.log(`sleeping for ${millis} milliseconds`);
  await new Promise(r => setTimeout(r, millis));
  console.log("woke up");
}
test('function', async () => {
  await wait(5000);
}, 70000);
Run Code Online (Sandbox Code Playgroud)


Jon*_*ona 6

我最近因为一个不同的原因遇到了这个问题:我正在使用 同步运行一些测试jest -i,它只是超时。无论出于何种原因,使用jest --runInBand(即使-i是别名)运行相同的测试都不会超时。


ale*_*ers 6

// In jest.setup.js
jest.setTimeout(30000)
Run Code Online (Sandbox Code Playgroud)

如果 Jest <= 23:

// In jest.config.js
module.exports = {
  setupTestFrameworkScriptFile: './jest.setup.js'
}
Run Code Online (Sandbox Code Playgroud)

如果 Jest > 23:

// In jest.config.js
module.exports = {
  setupFilesAfterEnv: ['./jest.setup.js']
}
Run Code Online (Sandbox Code Playgroud)


Ric*_*don 6

您还可以根据愚蠢的拼写错误获得超时错误。例如,这个看似无害的错误:

describe('Something', () => {
  it('Should do something', () => {
    expect(1).toEqual(1)
  })

  it('Should do nothing', something_that_does_not_exist => {
    expect(1).toEqual(1)
  })
})
Run Code Online (Sandbox Code Playgroud)

产生以下错误:

FAIL src/TestNothing.spec.js (5.427s)
  ? Something › Should do nothing

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
      
      at node_modules/jest-jasmine2/build/queue_runner.js:68:21
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:678:19)
Run Code Online (Sandbox Code Playgroud)

虽然发布的代码示例不受此影响,但它可能是其他地方失败的原因。另请注意,我没有在任何地方设置超时 - 无论是在此处还是在配置中。5000 ms 只是默认设置。


Nee*_*ani 5

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

玩笑 24.9


Xch*_*hai 5

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

我能够通过将 console.log() 语句放入我的承诺回调中来解决这个问题,并看到 console.log() 语句在笑话输出中运行。一旦我修复了预期断言,超时错误就消失了并且测试开始工作。

我花了很长时间才弄清楚这一点,希望这对需要阅读本文的人有所帮助。