Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure jest timeout once for all tests

Tags:

jestjs

ts-jest

According to the docs one can increase the default async timeout from 5000ms using the jest-object

More specifically, by using the jestsettimeouttimeout

The issue I am facing is I am running a series of tests against an API that is very slow, 5-15 second response times, configuring this jest object at the top of each test is painfully annoying.

Is it possible to declare these settings once before all test files are run?

like image 960
John Avatar asked Sep 21 '19 17:09

John


People also ask

How do I set Jest timeout globally?

To set a global timeout different than Jest's default, one must use a setupTestFrameworkScriptFile config and create a new file. This is a bit cumbersome, isn't it? It would also play nice with the API we have for already: jest. setTimeout(number) .

What is beforeAll and afterAll in Jest?

Jest provides beforeAll and afterAll . As with test / it it will wait for a promise to resolve, if the function returns a promise. beforeAll(() => { return new Promise(resolve => { // Asynchronous task // ...

Can I use setTimeout in Jest?

The native timer functions (i.e., setTimeout() , setInterval() , clearTimeout() , clearInterval() ) are less than ideal for a testing environment since they depend on real time to elapse. Jest can swap out timers with functions that allow you to control the passage of time.

How does Jest timeout work?

Note: The default timeout is 5 seconds. Note: If a promise is returned from test , Jest will wait for the promise to resolve before letting the test complete. Jest will also wait if you provide an argument to the test function, usually called done . This could be handy when you want to test callbacks.


1 Answers

OK, putting bits together:

  • Option "setupTestFrameworkScriptFile" was replaced by configuration "setupFilesAfterEnv", which supports multiple paths
  • https://jestjs.io/docs/en/jest-object#jestsettimeouttimeout
  • https://jestjs.io/docs/en/jest-object#jestdisableautomock

The Jest search box doesn't actually return anything when you search for: setupFilesAfterEnv

And docs talk about: setupTestFrameworkScriptFile (which also doesn't return anything on the search:/ )

Anyway, the docs leave you scratching your head but this works:

jest.config.js:

module.exports = {
  setupFilesAfterEnv: ['./setup.js'],

setup.js:

jest.setTimeout(10000); // in milliseconds

The jest folks should make it easier to find this information.

like image 69
John Avatar answered Sep 19 '22 10:09

John