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:
I specified the timeout to be 30000
Whether or not I get this error is seemingly very random
Can anyone guess why this is happening?
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: './jest.setup.js'
}
// jest.setup.js
jest.setTimeout(30000)
See this thread also
It should call the done function 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);
});
The answer to this question has changed as Jest has evolved. Current answer (March 2019):
You can override the timeout of any individual test by adding a third parameter to the
it
. ie.it('runs slow', () => {...}, 9999)
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)
- Like others have noted, and not directly related to this,
done
is not necessary with async/await approach.
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
});
});
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.
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 ¯\_(:/)_/¯
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
来源:https://stackoverflow.com/questions/49603939/async-callback-was-not-invoked-within-the-5000ms-timeout-specified-by-jest-setti