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条回答
劫难
2楼-- · 2020-01-27 02:00

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.
查看更多
forever°为你锁心
3楼-- · 2020-01-27 02:01

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
  });
});
查看更多
你好瞎i
4楼-- · 2020-01-27 02:07

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.

查看更多
我只想做你的唯一
5楼-- · 2020-01-27 02:08
// 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']
}
查看更多
爷的心禁止访问
6楼-- · 2020-01-27 02:13

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

查看更多
欢心
7楼-- · 2020-01-27 02:14

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);
});
查看更多
登录 后发表回答