Jest: Defining test timeout as a config parameter

Created on 21 May 2018  路  17Comments  路  Source: facebook/jest

馃殌 Feature Proposal

I'd like to propose introducing a new config parameter config called timeout, that would accept a number or a string.

Motivation

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).

Example

"jest": {
  "timeout": 60000
}

Pitch

Why does this feature belong in the
Jest core platform?

It is a config change, duh.

Feature Request

Most helpful comment

Having a flag like 'disable timeouts' on the cli to help debugging makes more sense to me than being able to set a timeout

All 17 comments

Makes sense. Should the name be testTimeout or something to be clear this is not a global test suite timeout, but per test?

I was taking that into consideration, and just decided to go jest.setTimeout() way, as we don't currently have mechanisms to limit per-suite or per-all-suites timeouts. We can add them later and name accordingly though.

Could this parameter be added to the CLI as well? Reason for this is that when I use a debugger to step through code, I would like to set it to infinite (or very high). But I don't want to change this setting in our config, because then this change might be committed by accident...

Or is it possible to point to a different config via another cli parameter?

Having a flag like 'disable timeouts' on the cli to help debugging makes more sense to me than being able to set a timeout

I can't believe this doesn't exist? How is it even possible to use a step debugger if every test times out after 5s?

So still no way to set default timeout value ?

@bluedusk you can always just drop somewhere jest.setTimeout(2137*1000); or something similar.

I think that users should be able to:

  • make timeout part of the config. Can be useful when all tests are expected to be slow (imagine using jest to drive e2e tests)
  • provide timeout as CLI parameter / disable timeouts - mostly useful when working with the debugger.

@thymikee what do you think about it? I can try implementing these.

@SimenB @krzkaczor so we could have a single --timeout flag that would take a number or Infinity for no-timeout at all, what do you think?

@thymikee I think 0 is more obvious (mocha does it as well).

Anyone working on this issue? If no, I pick up.

@ert78gb Is this issue covered by your PR?

Yes and it is landed in 24.9.0

Looks like we can close this :)

I am confused as to what this is supposed to do. I tried the config like this:

module.exports = {
  verbose: true
  projects: [
    {
      displayName: 'E2E',
      preset: 'jest-playwright-preset',
      rootDir: 'tests/',
      testTimeout: 100,
      setupFilesAfterEnv: [
        '<rootDir>/jest.setup.js',
      ],
    },
  ],
};

I have tests taking more than 100 ms obviously, but it still does not time out in any way. How come?

And what is the difference between the config testTimeout and jest.setTimeout(). Reading this thread it sounds like testTimeout is per test case i.e. per it() and that jest.setTimeout() is per "test file" since you refer to it as global. But in the docs it sounds like both are per "test case" (i.e. per it()), see https://jestjs.io/docs/en/jest-object#jestsettimeouttimeout and https://jestjs.io/docs/en/jest-object#jestsettimeouttimeout

It can be described both as 'per test' and 'per test file' - using jest.setTimeout() you set it for each individual test in the entire test file. If you know a way to make this clearer in the docs feel free to shoot over a PR 馃槃
Also, if tests exceed your timeout limit, note that it only applies to asynchronous operations. If a test synchronously takes a long time to run (heavy computations or lots of sync Node APIs, Jest can do nothing to stop it (that is not how the Node event loop works).

Good info! I have one since before, I suppose I should take one for the team and try to push a PR.

I'm running tests in asynchronous operations. Setting testTimeout: 100, like above still did not work to time out the test cases, but jest.setTimeout(100) did. I.e. it doesn't work, at least not with projects.

Was this page helpful?
0 / 5 - 0 ratings