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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49603939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:24:56  来源:igfitidea点击:

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

javascriptautomated-testsjestjspuppeteer

提问by Asool

I'm using puppeteer and jest to run some front end tests.

我正在使用 puppeteer 和 jest 来运行一些前端测试。

My tests look as follows:

我的测试如下:

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

Sometimes, when I run the tests, everything works as expectedly. Other times, I get an error:

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

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)

This is strange because:

这很奇怪,因为:

  1. I specified the timeout to be 30000

  2. Whether or not I get this error is seemingly very random

  1. 我将超时指定为 30000

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

Can anyone guess why this is happening?

谁能猜到为什么会这样?

回答by Tarun Lalwani

So the timeout you specify here needs to be shorter than the default timeout.

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

The default timeout is 5000and the framework by default is jasminein case of jest. You can specify the timeout inside the test by adding

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

jest.setTimeout(30000);

But this would be specific to the test. Or you can setup the config file for the framework.

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

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

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

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

See this thread also

另请参阅此线程

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

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

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

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

回答by schrodinger's code

It should call the async/awaitwhen it is 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);
});

回答by ndp

The answer to this question has changed as Jest has evolved. Current answer (March 2019):

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

  1. You can override the timeout of any individual test by adding a third parameter to the it. ie. it('runs slow', () => {...}, 9999)

  2. You can change the default using jest.setTimeout. To do this:

  1. 您可以通过将第三个参数添加到it. IE。it('runs slow', () => {...}, 9999)

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

 // config
   "setupFilesAfterEnv": [  // NOT setupFiles
     "./src/jest/defaultTimeout.js"
   ],

and

// File: src/jest/defaultTimeout.js
/* global jest */
jest.setTimeout(1000)
  1. Like others have noted, and not directly related to this, doneis not necessary with async/await approach.
  1. 就像其他人指出的那样,并且与此没有直接关系done,async/await 方法没有必要。

回答by Roman

I would like to add (this is a bit long for a comment) that even with a timeout of 3000my tests would still sometimes (randomly) fail with

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

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

Thanks to @Tarun's great answer, I think the shortest way to fix a lot of tests is:

感谢@Tarun 的精彩回答,我认为修复大量测试的最短方法是:

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

  test('best jest test fest', async () => {
    // blah
  });
});

回答by e-shfiyut

This is a relatively new update but it is much more straight forward. If you are using jest 24.9.0 or higher you can just add testTimeoutto your config:

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

// in jest.config.js
module.exports = {
  testTimeout: 30000
}

回答by ZenVentzi

Make sure to invoke done();on callbacks or it won't simply pass the test.

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

beforeAll((done /* call it or remove it*/) => {
  done(); // calling it
});

Applies to all other functions that have a done() callback.

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

回答by Mr. 14

For jest 24.9+, You can also set timeout from the command line by adding --testTimeout

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

Here's an excerpt from its docs

这是其文档的摘录

--testTimeout=<number>
Default timeout of a test in milliseconds. Default value: 5000.

回答by Jona

I recently ran into this issue for a different reason: I was running some tests synchronously using jest -i, and it would just timeout. For whatever reasoning, running the same tests using jest --runInBand(even though -iis meant to be an alias) doesn't time out.

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

Maybe this will help someone ˉ\_(:/)_/ˉ

也许这会帮助某人 ˉ\_(:/)_/ˉ

回答by Neeraj Sewani

The timeout problem occurs when either network is slow or many network calls are made using await, these scenarios exceed the default timeout i.e 5000ms. To avoid the timeout error simply increase the timeout of globals that support a timeout. A list of globals and their signature can be found here.
For Jest 24.9

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

回答by alexrogers

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

If on Jest <= 23:

如果在 Jest <= 23 上:

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

If on Jest > 23:

如果在 Jest > 23 上:

// in jest.config.js
module.exports = {
  setupFilesAfterEnv: ['./jest.setup.js']
}