Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule to check placement of jest.setTimeout #1366

Closed
michal-kocarek opened this issue Apr 18, 2023 · 3 comments · Fixed by #1425
Closed

Add rule to check placement of jest.setTimeout #1366

michal-kocarek opened this issue Apr 18, 2023 · 3 comments · Fixed by #1425

Comments

@michal-kocarek
Copy link

michal-kocarek commented Apr 18, 2023

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
@G-Rath
Copy link
Collaborator

G-Rath commented Apr 18, 2023

Sounds reasonable - we could call it something like no-confusing-set-timeout

@eryue0220
Copy link
Contributor

I would like to take it.

@github-actions
Copy link

🎉 This issue has been resolved in version 27.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants