I am using Jest for testing my rest API. Most of my API endpoints take more than 5 seconds to return, so I need to increase the default timeout of 5000ms specified in Jest.
I have 2 test files, suppose: foo.test.js bar.test.js
Now, I can get rid of the timeout error by following 2 ways:
test
or it
methods. This works fine, but I need to update all of my existing test cases.jest.setTimeout(20000);
at the top of each of my test files. This also works fine. But still, it requires me to update all my existing test files.I found out that I can instead specify jest.setTimeout(20000);
in a separate file, and point to that in setupFiles
configuration option for jest in package.json. I did this but the timeout error of exceeding 5000ms started coming up again. My setupFile is being loaded correctly, which I checked by adding some console.log
statements to this. But it seems jest.setTimeout(20000)
is not having any effect on the test cases.
Use jest. setTimeout(newTimeout) to increase the timeout value, if this is a long-running test." 1)Increased jest. setTimeout(30000) from 30000 to 60000.
beforeEach(fn, timeout) If the function returns a promise or is a generator, Jest waits for that promise to resolve before running the test. Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds.
setupFiles will be executed. before the test framework is installed in the environment. setupFilesAfterEnv will be executed. after the test framework has been installed in the environment. That's why the name has AfterEnv.
rootDir [string] Default: The root of the directory containing the package.json or the pwd if no package.json is found. Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
From the docs for setupFilesAfterEnv
:
setupFiles
executes before the test framework is installed in the environment
(That information should probably be added to the setupFiles
section of the docs as well.)
setupFiles
runs before the test framework is installed. I'm a bit surprised that jest.setTimeout
is defined within a setup file. In any case, calling jest.setTimeout
doesn't work right from within a file in setupFiles
.
Instead, use setupFilesAfterEnv
which runs
immediately after the test framework has been installed in the environment
Note that setupFilesAfterEnv
was introduced in version 24.0.0
.
If you are using an earlier version of Jest
use the older setupTestFrameworkScriptFile
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With