-
Notifications
You must be signed in to change notification settings - Fork 242
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
feat: add no-confusing-set-time rule #1425
Merged
G-Rath
merged 14 commits into
jest-community:main
from
eryue0220:rule/no-confusing-set-timeout
Sep 15, 2023
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b0d4ff4
feat: add no-confusing-set-time rule
eryue0220 a1eec07
Merge branch 'main' into rule/no-confusing-set-timeout
eryue0220 0a98694
fix: cr problems
eryue0220 7e2ce4b
fix: ci
eryue0220 b21df04
Merge branch 'main' into rule/no-confusing-set-timeout
eryue0220 59da5f3
feat: reactor rule
eryue0220 25b2d93
chore: upgrade
eryue0220 a2e9080
Merge branch 'main' into rule/no-confusing-set-timeout
eryue0220 b0338c0
chore: fix lint
SimenB c39b755
fix: ci problems
eryue0220 40fa046
fix: cr problems
eryue0220 80f525e
fix: ci
eryue0220 382ddbb
Merge branch 'main' into rule/no-confusing-set-timeout
G-Rath c5e73ee
docs: regenerate
G-Rath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,63 @@ | ||||||||||||||||
# Disallow using confusing setTimeout in test (`no-confusing-set-timeout`) | ||||||||||||||||
|
||||||||||||||||
<!-- end auto-generated rule header --> | ||||||||||||||||
|
||||||||||||||||
`jest.setTimeout` can be called multiple times anywhere within a single test | ||||||||||||||||
file. However, only the last call will have an effect, and it will actually be | ||||||||||||||||
invoked before any other jest functions. | ||||||||||||||||
|
||||||||||||||||
## Rule details | ||||||||||||||||
|
||||||||||||||||
This rule describes some tricky ways about `jest.setTimeout` that should not | ||||||||||||||||
SimenB marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
recommend in Jest: | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this:
Suggested change
|
||||||||||||||||
|
||||||||||||||||
- should set `jest.setTimeout` in any testsuite methods before(such as | ||||||||||||||||
SimenB marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
`describe`, `test` or `it`); | ||||||||||||||||
- should set `jest.setTimeout` in global scope. | ||||||||||||||||
- should only call `jest.setTimeout` once in a single test file; | ||||||||||||||||
|
||||||||||||||||
Examples of **incorrect** code for this rule: | ||||||||||||||||
|
||||||||||||||||
```js | ||||||||||||||||
describe('test foo', () => { | ||||||||||||||||
jest.setTimeout(1000); | ||||||||||||||||
it('test-description', () => { | ||||||||||||||||
// test logic; | ||||||||||||||||
}); | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
describe('test bar', () => { | ||||||||||||||||
it('test-description', () => { | ||||||||||||||||
jest.setTimeout(1000); | ||||||||||||||||
// test logic; | ||||||||||||||||
}); | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
test('foo-bar', () => { | ||||||||||||||||
jest.setTimeout(1000); | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
describe('unit test', () => { | ||||||||||||||||
beforeEach(() => { | ||||||||||||||||
jest.setTimeout(1000); | ||||||||||||||||
}); | ||||||||||||||||
}); | ||||||||||||||||
``` | ||||||||||||||||
|
||||||||||||||||
Examples of **correct** code for this rule: | ||||||||||||||||
|
||||||||||||||||
```js | ||||||||||||||||
jest.setTimeout(500); | ||||||||||||||||
test('test test', () => { | ||||||||||||||||
// do some stuff | ||||||||||||||||
}); | ||||||||||||||||
``` | ||||||||||||||||
|
||||||||||||||||
```js | ||||||||||||||||
jest.setTimeout(1000); | ||||||||||||||||
describe('test bar bar', () => { | ||||||||||||||||
it('test-description', () => { | ||||||||||||||||
// test logic; | ||||||||||||||||
}); | ||||||||||||||||
}); | ||||||||||||||||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
import { TSESLint } from '@typescript-eslint/utils'; | ||
import dedent from 'dedent'; | ||
import rule from '../no-confusing-set-timeout'; | ||
import { espreeParser } from './test-utils'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: espreeParser, | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-confusing-set-timeout', rule, { | ||
G-Rath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
valid: [ | ||
dedent` | ||
jest.setTimeout(1000); | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
`, | ||
dedent` | ||
jest.setTimeout(1000); | ||
window.setTimeout(6000) | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('test foo', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
`, | ||
{ | ||
code: dedent` | ||
import { handler } from 'dep/mod'; | ||
jest.setTimeout(800); | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
}, | ||
dedent` | ||
function handler() {} | ||
jest.setTimeout(800); | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
`, | ||
dedent` | ||
const { handler } = require('dep/mod'); | ||
jest.setTimeout(800); | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
`, | ||
dedent` | ||
jest.setTimeout(1000); | ||
window.setTimeout(60000); | ||
`, | ||
'window.setTimeout(60000);', | ||
'setTimeout(1000);', | ||
dedent` | ||
jest.setTimeout(1000); | ||
test('test case', () => { | ||
setTimeout(() => { | ||
Promise.resolv(); | ||
}, 5000); | ||
}); | ||
`, | ||
dedent` | ||
test('test case', () => { | ||
setTimeout(() => { | ||
Promise.resolv(); | ||
}, 5000); | ||
}); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
jest.setTimeout(1000); | ||
setTimeout(1000); | ||
window.setTimeout(1000); | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
jest.setTimeout(800); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'orderSetTimeout', | ||
line: 9, | ||
column: 1, | ||
}, | ||
{ | ||
messageId: 'multipleSetTimeouts', | ||
line: 9, | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
describe('A', () => { | ||
jest.setTimeout(800); | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'globalSetTimeout', | ||
line: 2, | ||
column: 3, | ||
}, | ||
{ | ||
messageId: 'orderSetTimeout', | ||
line: 2, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
describe('B', () => { | ||
it('B.1', async () => { | ||
await new Promise((resolve) => { | ||
jest.setTimeout(1000); | ||
setTimeout(resolve, 10000).unref(); | ||
}); | ||
}); | ||
it('B.2', async () => { | ||
await new Promise((resolve) => { setTimeout(resolve, 10000).unref(); }); | ||
}); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'globalSetTimeout', | ||
line: 4, | ||
column: 7, | ||
}, | ||
{ | ||
messageId: 'orderSetTimeout', | ||
line: 4, | ||
column: 7, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
test('test-suite', () => { | ||
jest.setTimeout(1000); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'globalSetTimeout', | ||
line: 2, | ||
column: 3, | ||
}, | ||
{ | ||
messageId: 'orderSetTimeout', | ||
line: 2, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
jest.setTimeout(1000); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'orderSetTimeout', | ||
line: 6, | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
import { jest } from '@jest/globals'; | ||
{ | ||
jest.setTimeout(800); | ||
} | ||
describe('A', () => { | ||
beforeEach(async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.1', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
it('A.2', async () => { await new Promise(resolve => { setTimeout(resolve, 10000).unref(); });}); | ||
}); | ||
`, | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [ | ||
{ | ||
messageId: 'globalSetTimeout', | ||
line: 3, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
jest.setTimeout(800); | ||
jest.setTimeout(900); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'multipleSetTimeouts', | ||
line: 2, | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
expect(1 + 2).toEqual(3); | ||
jest.setTimeout(800); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'orderSetTimeout', | ||
line: 2, | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.