-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create
max-expects
rule (#1166)
* feat: create max-expects * fix: fix test * chore: update README * fix: set proper minimum * refactor: remove unnecessary functions * refactor: reduce if * refactor: remove unnecessary type check * fix: incorrect error reported in case of multiple test blocks * chore: typo * refactor: remove unnecessary type check * chore: remove unneeded return Co-authored-by: Gareth Jones <jones258@gmail.com>
- Loading branch information
Showing
6 changed files
with
430 additions
and
1 deletion.
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,74 @@ | ||
# Enforces a maximum number assertion calls in a test body (`max-expects`) | ||
|
||
As more assertions are made, there is a possible tendency for the test to be | ||
more likely to mix multiple objectives. To avoid this, this rule reports when | ||
the maximum number of assertions is exceeded. | ||
|
||
## Rule Details | ||
|
||
This rule enforces a maximum number of `expect()` calls. | ||
|
||
The following patterns are considered warnings (with the default option of | ||
`{ "max": 5 } `): | ||
|
||
```js | ||
test('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
|
||
it('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
``` | ||
|
||
The following patterns are **not** considered warnings (with the default option | ||
of `{ "max": 5 } `): | ||
|
||
```js | ||
test('shout pass'); | ||
|
||
test('shout pass', () => {}); | ||
|
||
test.skip('shout pass', () => {}); | ||
|
||
test('should pass', function () { | ||
expect(true).toBeDefined(); | ||
}); | ||
|
||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
```json | ||
{ | ||
"jest/max-expects": [ | ||
"error", | ||
{ | ||
"max": 5 | ||
} | ||
] | ||
} | ||
``` | ||
|
||
### `max` | ||
|
||
Enforces a maximum number of `expect()`. | ||
|
||
This has a default value of `5`. |
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
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,284 @@ | ||
import { TSESLint } from '@typescript-eslint/utils'; | ||
import dedent from 'dedent'; | ||
import rule from '../max-expects'; | ||
import { espreeParser } from './test-utils'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: espreeParser, | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
}, | ||
}); | ||
|
||
ruleTester.run('max-expects', rule, { | ||
valid: [ | ||
`test('should pass')`, | ||
`test('should pass', () => {})`, | ||
`test.skip('should pass', () => {})`, | ||
dedent` | ||
test('should pass', function () { | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
dedent` | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
dedent` | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
// expect(true).toBeDefined(); | ||
}); | ||
`, | ||
dedent` | ||
it('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
dedent` | ||
test('should pass', async () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
dedent` | ||
describe('test', () => { | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
}); | ||
`, | ||
dedent` | ||
test.each(['should', 'pass'], () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
dedent` | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
{ | ||
code: dedent` | ||
test('should pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
options: [ | ||
{ | ||
max: 10, | ||
}, | ||
], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
test('should not pass', function () { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 7, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
test('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 7, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
it('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 7, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
it('should not pass', async () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 7, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
test('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
test('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 7, | ||
column: 3, | ||
}, | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 15, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
describe('test', () => { | ||
test('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 8, | ||
column: 5, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
test.each(['should', 'not', 'pass'], () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 7, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent` | ||
test('should not pass', () => { | ||
expect(true).toBeDefined(); | ||
expect(true).toBeDefined(); | ||
}); | ||
`, | ||
options: [ | ||
{ | ||
max: 1, | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
messageId: 'exceededMaxAssertion', | ||
line: 3, | ||
column: 3, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.