Async callback was not invoked within the 5000ms t

2020-01-27 01:30发布

问题:

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?

回答1:

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



回答2:

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


回答3:

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

  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:

 // 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, done is not necessary with async/await approach.


回答4:

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

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:

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

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


回答5:

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

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

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



回答6:

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.


回答7:

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 ¯\_(:/)_/¯



回答8:

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
}


回答9:

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



回答10:

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



回答11:

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

If on Jest <= 23:

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

If on Jest > 23:

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


回答12:

In case someone doesn't fix the problem use methods above, I fixed mine by surround the async func by an arrow function. As in:

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