Skip to content

Add rule to check placement of jest.setTimeout #1366

Closed

Description

The jest.setTimeout() sets default timeout for whole test file regardless of its actual placement in the file. The Jest API documentation mentions it, but it's not clear how this works under the hood. In fact, only last call applies and sets the timeout for all tests before they are started. As it might be tempting to place that command anywhere in the file, it would be useful to create an ESLint rule that:

  • checks if jest.setTimeout() is placed in global scope only (this itself would have immense value)
  • checks if jest.setTimeout() is only once in the file (nice to have)
  • checks if jest.setTimeout() is placed before any Jest test methods (before*, after*, test*, it*, describe*) (nice to have)

As an example, in this test file, the timeout for all tests is set to 800ms:

jest.setTimeout(1000);

describe('A', () => {
  beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); // applied timeout: 800ms

  it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); // applied timeout: 800ms

  jest.setTimeout(2000);

  it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); // applied timeout: 800ms
});

jest.setTimeout(1500);

describe('B', () => {
  it('B.1', async () => { await new Promise(resolve => {
    jest.setTimeout(1000);
    setTimeout(resolve, 10000).unref();
  });}); // applied timeout: 800ms

  it('B.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); // applied timeout: 800ms

  jest.setTimeout(500);
});

jest.setTimeout(800); // only value that applies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions