Skip to content

Commit

Permalink
feat(new rule): setup prefer expect assertions (#326)
Browse files Browse the repository at this point in the history
* feat(new rule): setup prefer expect assertions

* chore(vitest arning): remove visted module warning
  • Loading branch information
veritem committed Dec 17, 2023
1 parent 37261f0 commit ec2952c
Show file tree
Hide file tree
Showing 12 changed files with 1,016 additions and 367 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Expand Up @@ -9,6 +9,7 @@
],
"no-case-declarations": 0,
"no-tabs": "off",
"no-mixed-spaces-and-tabs": "off",
"vitest/unbound-method": "off"
}
}
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -152,6 +152,7 @@ export default [
| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | 🌐 | 🔧 | |
| [prefer-each](docs/rules/prefer-each.md) | Prefer `each` rather than manual loops | | 🌐 | | |
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in quality matchers | | 🌐 | | 💡 |
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using expect assertions instead of callbacks | | 🌐 | | 💡 |
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Suggest using `expect().resolves` over `expect(await ...)` syntax | | 🌐 | 🔧 | |
| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in consistent order | | 🌐 | | |
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | 🌐 | | |
Expand Down
116 changes: 116 additions & 0 deletions docs/rules/prefer-expect-assertions.md
@@ -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');
});
});
```
4 changes: 2 additions & 2 deletions fixtures/package.json
Expand Up @@ -12,9 +12,9 @@
"license": "MIT",
"dependencies": {
"eslint-plugin-vitest": "link:../",
"vitest": "^1.0.2"
"vitest": "^1.0.4"
},
"devDependencies": {
"eslint": "^8.55.0"
"eslint": "^8.56.0"
}
}
18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -40,26 +40,26 @@
"tsc": "tsc --noEmit"
},
"devDependencies": {
"@babel/types": "^7.23.5",
"@babel/types": "^7.23.6",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/rule-tester": "^6.13.2",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/rule-tester": "^6.14.0",
"@veritem/eslint-config": "^0.0.11",
"bumpp": "^9.2.0",
"bumpp": "^9.2.1",
"concurrently": "^8.2.2",
"eslint": "^8.55.0",
"eslint": "^8.56.0",
"eslint-doc-generator": "^1.6.1",
"eslint-plugin-eslint-plugin": "^5.1.1",
"eslint-plugin-eslint-plugin": "^5.2.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-vitest": "^0.3.10",
"eslint-plugin-vitest": "^0.3.17",
"eslint-remote-tester": "^3.0.1",
"eslint-remote-tester-repositories": "^1.0.1",
"ts-node": "^10.9.2",
"tsx": "^4.6.2",
"typescript": "^5.3.3",
"unbuild": "^2.0.0",
"vitest": "^1.0.2"
"vitest": "^1.0.4"
},
"engines": {
"node": "^18.0.0 || >= 20.0.0"
Expand All @@ -77,6 +77,6 @@
}
},
"dependencies": {
"@typescript-eslint/utils": "^6.13.2"
"@typescript-eslint/utils": "^6.14.0"
}
}

0 comments on commit ec2952c

Please sign in to comment.