Async callback was not invoked within the 5000ms t

2020-01-27 01:41发布

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

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

Can anyone guess why this is happening?

12条回答
【Aperson】
2楼-- · 2020-01-27 01:49

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

Here's an excerpt from its docs

--testTimeout=<number>
Default timeout of a test in milliseconds. Default value: 5000.
查看更多
虎瘦雄心在
3楼-- · 2020-01-27 01:53

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 testTimeout to your config:

// in jest.config.js
module.exports = {
  testTimeout: 30000
}
查看更多
迷人小祖宗
4楼-- · 2020-01-27 01:55

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

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

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

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

查看更多
放我归山
5楼-- · 2020-01-27 01:55

It should call the async/await when it is async from test.

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);
});
查看更多
放我归山
6楼-- · 2020-01-27 01:58

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 -i is meant to be an alias) doesn't time out.

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

查看更多
不美不萌又怎样
7楼-- · 2020-01-27 01:58

For those who are looking an explanation about jest --runInBand you can go to the documentation Running puppeteer in CI environments https://github.com/smooth-code/jest-puppeteer

查看更多
登录 后发表回答