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?
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:and
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 withThanks to @Tarun's great answer, I think the shortest way to fix a lot of tests is:
Make sure to invoke
done();
on callbacks or it won't simply pass the test.Applies to all other functions that have a done() callback.
If on Jest <= 23:
If on Jest > 23:
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
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: