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?
For jest 24.9+, You can also set timeout from the command line by adding
--testTimeout
Here's an excerpt from its docs
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:So the timeout you specify here needs to be shorter than the default timeout.
The default timeout is
5000
and the framework by default isjasmine
in case ofjest
. You can specify the timeout inside the test by addingBut 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
See this thread also
https://github.com/facebook/jest/issues/5055
https://github.com/facebook/jest/issues/652
It should call the
async/await
when it is async from test.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 usingjest --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