Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest.setTimeout is not working in setupFiles

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:

  1. Setting timeout values greater than 5000, as the 3rd argument to test or it methods. This works fine, but I need to update all of my existing test cases.
  2. Adding 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.

like image 633
Mohit Bhardwaj Avatar asked Feb 20 '19 08:02

Mohit Bhardwaj


People also ask

How do I set Jest timeout?

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.

How does Jest timeout work?

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.

What is setupFilesAfterEnv?

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.

What is rootDir in Jest?

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.


1 Answers

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.

like image 131
Brian Adams Avatar answered Sep 22 '22 00:09

Brian Adams