-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(new rule): setup prefer expect assertions (#326)
* feat(new rule): setup prefer expect assertions * chore(vitest arning): remove visted module warning
- Loading branch information
Showing
12 changed files
with
1,016 additions
and
367 deletions.
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
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,116 @@ | ||
# Suggest using expect assertions instead of callbacks (`vitest/prefer-expect-assertions`) | ||
|
||
⚠️ This rule _warns_ in the 🌐 `all` config. | ||
|
||
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Ensure every test to have either `expect.assertions(<number of assertions>)` OR | ||
`expect.hasAssertions()` as its first expression. | ||
|
||
This will warn if a test has no assertions, or if it has assertions but they are not the first expression. | ||
|
||
## Examples | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
test('no assertions', () => { | ||
// ... | ||
}); | ||
|
||
test('assertions not first', () => { | ||
expect(true).toBe(true); | ||
// ... | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
test('assertions first', () => { | ||
expect.assertions(1); | ||
// ... | ||
}); | ||
|
||
test('assertions first', () => { | ||
expect.hasAssertions(); | ||
// ... | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
`onlyFunctionsWithAsyncKeyword` (default: `false`) | ||
|
||
When `true`, only functions with the `async` keyword will be checked. | ||
|
||
when this option is enabled the following code will be considered incorrect: | ||
|
||
```js | ||
test('assertions first', () => { | ||
const data = await fetchData(); | ||
expect(data).toBe('peanut butter'); | ||
}); | ||
``` | ||
|
||
To fix this, you'll need to add `expect.assertions(1)` or `expect.hasAssertions()` as the first expression: | ||
|
||
```js | ||
test('assertions first', () => { | ||
expect.assertions(1); | ||
const data = await fetchData(); | ||
expect(data).toBe('peanut butter'); | ||
}); | ||
``` | ||
|
||
`onlyFunctionsWithExpectInLoop` (default: `false`) | ||
|
||
When `true`, only functions with `expect` in a loop will be checked. | ||
|
||
when this option is enabled the following code will be considered incorrect: | ||
|
||
```js | ||
test('assertions first', () => { | ||
for (let i = 0; i < 10; i++) { | ||
expect(i).toBeLessThan(10); | ||
} | ||
}); | ||
``` | ||
|
||
To fix this, you'll need to add `expect.assertions(1)` or `expect.hasAssertions()` as the first expression: | ||
|
||
```js | ||
test('assertions first', () => { | ||
expect.hasAssertions(); | ||
for (let i = 0; i < 10; i++) { | ||
expect(i).toBeLessThan(10); | ||
} | ||
}); | ||
``` | ||
|
||
`onlyFunctionsWithExpectInCallback` | ||
|
||
When `true`, only functions with `expect` in a callback will be checked. | ||
|
||
when this option is enabled the following code will be considered incorrect: | ||
|
||
```js | ||
test('assertions first', () => { | ||
fetchData((data) => { | ||
expect(data).toBe('peanut butter'); | ||
}); | ||
}); | ||
``` | ||
|
||
To fix this, you'll need to add `expect.assertions(1)` or `expect.hasAssertions()` as the first expression: | ||
|
||
```js | ||
test('assertions first', () => { | ||
expect.assertions(1); | ||
fetchData((data) => { | ||
expect(data).toBe('peanut butter'); | ||
}); | ||
}); | ||
``` |
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
Oops, something went wrong.