36

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?

skyboyer
  • 22,209
  • 7
  • 57
  • 64

4 Answers4

38

Jest offers a testTimeout configuration option you can add to your package.json:

  "jest": {
    "testTimeout": 15000,
  }
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Geng Jiawen
  • 8,904
  • 3
  • 48
  • 37
33

OK, putting bits together:

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.

  • 19
    If someone stumbles across this post. The jest folks added a new config option called [`testTimeout`](https://jestjs.io/docs/en/configuration#testtimeout-number) which would do the same. :) – lumio Jan 25 '21 at 16:18
22

Use testTimeout. In yourjest.config.js (or similar), add the following:

const SECONDS = 1000;

module.exports = {
  testTimeout: 60 * SECONDS
}
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
fIwJlxSzApHEZIl
  • 11,861
  • 6
  • 62
  • 71
7

If you are working with react and initializing you app using create-react-app, then under your src/ directory you should have a file named setupTests.js. Here you can setup a global timeout for all of your tests just by insert this line after the import statement for @testing-libary

jest.setTimeout(15000); // in milliseconds
Artan M.
  • 817
  • 1
  • 11
  • 16