eslint-plugin-jest 25.0.0-next.4 → 25.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +138 -0
- package/README.md +71 -49
- package/docs/rules/max-nested-describe.md +4 -5
- package/docs/rules/no-conditional-expect.md +57 -3
- package/docs/rules/no-deprecated-functions.md +5 -0
- package/docs/rules/no-done-callback.md +3 -3
- package/docs/rules/no-standalone-expect.md +3 -3
- package/docs/rules/no-test-return-statement.md +1 -2
- package/docs/rules/prefer-expect-resolves.md +53 -0
- package/docs/rules/{lowercase-name.md → prefer-lowercase-title.md} +1 -1
- package/docs/rules/prefer-to-be.md +53 -0
- package/docs/rules/require-hook.md +149 -0
- package/docs/rules/require-top-level-describe.md +28 -0
- package/docs/rules/{valid-describe.md → valid-describe-callback.md} +1 -1
- package/docs/rules/valid-expect-in-promise.md +55 -14
- package/docs/rules/valid-title.md +30 -2
- package/lib/index.js +2 -3
- package/lib/rules/detectJestVersion.js +29 -0
- package/lib/rules/no-deprecated-functions.js +9 -27
- package/lib/rules/no-identical-title.js +3 -3
- package/lib/rules/no-large-snapshots.js +1 -3
- package/lib/rules/prefer-expect-resolves.js +48 -0
- package/lib/rules/{lowercase-name.js → prefer-lowercase-title.js} +20 -1
- package/lib/rules/prefer-to-be.js +129 -0
- package/lib/rules/require-hook.js +87 -0
- package/lib/rules/require-top-level-describe.js +40 -6
- package/lib/rules/utils.js +6 -4
- package/lib/rules/{valid-describe.js → valid-describe-callback.js} +0 -0
- package/lib/rules/valid-expect-in-promise.js +252 -68
- package/lib/rules/valid-expect.js +10 -4
- package/lib/rules/valid-title.js +47 -47
- package/package.json +5 -5
- package/docs/rules/prefer-to-be-null.md +0 -33
- package/docs/rules/prefer-to-be-undefined.md +0 -33
- package/lib/rules/prefer-to-be-null.js +0 -67
- package/lib/rules/prefer-to-be-undefined.js +0 -67
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
|
+
# [25.0.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.7.0...v25.0.0) (2021-10-10)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* stop testing ESLint 5 ([#893](https://github.com/jest-community/eslint-plugin-jest/issues/893)) ([47a0138](https://github.com/jest-community/eslint-plugin-jest/commit/47a0138856e6247cde00b17682e49865b8f5a1f6))
|
|
7
|
+
* stop testing on Node 10 and 15 ([#891](https://github.com/jest-community/eslint-plugin-jest/issues/891)) ([bcd8d11](https://github.com/jest-community/eslint-plugin-jest/commit/bcd8d112fcd98a7652c767bd246d05101979239c))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* add `prefer-to-be` to style ruleset ([2a3376f](https://github.com/jest-community/eslint-plugin-jest/commit/2a3376fc9f5fe60d03d9aad0c4e5c7c423487e60))
|
|
13
|
+
* **lowercase-name:** rename to `prefer-lowercase-title` ([b860084](https://github.com/jest-community/eslint-plugin-jest/commit/b8600841e371d5d9f36be4e50e53252fd8f62734))
|
|
14
|
+
* **prefer-to-be-null:** remove rule ([809bcda](https://github.com/jest-community/eslint-plugin-jest/commit/809bcda12c555a24c764d152bcac9814ea55e72f))
|
|
15
|
+
* **prefer-to-be-undefined:** remove rule ([3434d9b](https://github.com/jest-community/eslint-plugin-jest/commit/3434d9bd22b92bace6e0a50e2c72b401ac17704d))
|
|
16
|
+
* remove deprecated rules ([#661](https://github.com/jest-community/eslint-plugin-jest/issues/661)) ([e8f16ec](https://github.com/jest-community/eslint-plugin-jest/commit/e8f16ec0e204a94a0e549cb9b415b3c6c8981aee))
|
|
17
|
+
* **valid-describe:** rename to `valid-describe-callback` ([f3e9e9a](https://github.com/jest-community/eslint-plugin-jest/commit/f3e9e9a64e183a0fb8af3436611a7f70366a528d))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### BREAKING CHANGES
|
|
21
|
+
|
|
22
|
+
* **valid-describe:** renamed `valid-describe` to `valid-describe-callback`
|
|
23
|
+
* **lowercase-name:** renamed `lowercase-name` to `prefer-lowercase-title`
|
|
24
|
+
* **prefer-to-be-undefined:** removed `prefer-to-be-undefined` rule
|
|
25
|
+
* **prefer-to-be-null:** removed `prefer-to-be-null` rule
|
|
26
|
+
* recommend `prefer-to-be` rule
|
|
27
|
+
* Removes rules `no-expect-resolves`, `no-truthy-falsy`, `no-try-expect`, and `prefer-inline-snapshots`
|
|
28
|
+
* Drop support for ESLint 5
|
|
29
|
+
* Drop support for Node 10 and 15
|
|
30
|
+
|
|
31
|
+
# [25.0.0-next.7](https://github.com/jest-community/eslint-plugin-jest/compare/v25.0.0-next.6...v25.0.0-next.7) (2021-10-10)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### Features
|
|
35
|
+
|
|
36
|
+
* add `prefer-to-be` to style ruleset ([2a3376f](https://github.com/jest-community/eslint-plugin-jest/commit/2a3376fc9f5fe60d03d9aad0c4e5c7c423487e60))
|
|
37
|
+
* **lowercase-name:** rename to `prefer-lowercase-title` ([b860084](https://github.com/jest-community/eslint-plugin-jest/commit/b8600841e371d5d9f36be4e50e53252fd8f62734))
|
|
38
|
+
* **prefer-to-be-null:** remove rule ([809bcda](https://github.com/jest-community/eslint-plugin-jest/commit/809bcda12c555a24c764d152bcac9814ea55e72f))
|
|
39
|
+
* **prefer-to-be-undefined:** remove rule ([3434d9b](https://github.com/jest-community/eslint-plugin-jest/commit/3434d9bd22b92bace6e0a50e2c72b401ac17704d))
|
|
40
|
+
* **valid-describe:** rename to `valid-describe-callback` ([f3e9e9a](https://github.com/jest-community/eslint-plugin-jest/commit/f3e9e9a64e183a0fb8af3436611a7f70366a528d))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
### BREAKING CHANGES
|
|
44
|
+
|
|
45
|
+
* **valid-describe:** renamed `valid-describe` to `valid-describe-callback`
|
|
46
|
+
* **lowercase-name:** renamed `lowercase-name` to `prefer-lowercase-title`
|
|
47
|
+
* **prefer-to-be-undefined:** removed `prefer-to-be-undefined` rule
|
|
48
|
+
* **prefer-to-be-null:** removed `prefer-to-be-null` rule
|
|
49
|
+
* recommend `prefer-to-be` rule
|
|
50
|
+
|
|
51
|
+
# [25.0.0-next.6](https://github.com/jest-community/eslint-plugin-jest/compare/v25.0.0-next.5...v25.0.0-next.6) (2021-10-10)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
### Bug Fixes
|
|
55
|
+
|
|
56
|
+
* **lowercase-name:** consider skip and only prefixes for ignores ([#923](https://github.com/jest-community/eslint-plugin-jest/issues/923)) ([8716c24](https://github.com/jest-community/eslint-plugin-jest/commit/8716c24678ea7dc7c9f692b573d1ea19a67efd84))
|
|
57
|
+
* **prefer-to-be:** don't consider RegExp literals as `toBe`-able ([#922](https://github.com/jest-community/eslint-plugin-jest/issues/922)) ([99b6d42](https://github.com/jest-community/eslint-plugin-jest/commit/99b6d429e697d60645b4bc64ee4ae34d7016118c))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
### Features
|
|
61
|
+
|
|
62
|
+
* create `require-hook` rule ([#929](https://github.com/jest-community/eslint-plugin-jest/issues/929)) ([6204b31](https://github.com/jest-community/eslint-plugin-jest/commit/6204b311e849b51a0e4705015575139f590ae7a4))
|
|
63
|
+
* deprecate `prefer-to-be-null` rule ([4db9161](https://github.com/jest-community/eslint-plugin-jest/commit/4db91612e988e84ac2facbfe466331b22eeccec9))
|
|
64
|
+
* deprecate `prefer-to-be-undefined` rule ([fa08f09](https://github.com/jest-community/eslint-plugin-jest/commit/fa08f0944e89915fb215bbeff970f12459121cb8))
|
|
65
|
+
* **valid-expect-in-promise:** re-implement rule ([#916](https://github.com/jest-community/eslint-plugin-jest/issues/916)) ([7a49c58](https://github.com/jest-community/eslint-plugin-jest/commit/7a49c5831e3d85a60c11e385203b8f83d98ad580))
|
|
66
|
+
|
|
67
|
+
# [25.0.0-next.5](https://github.com/jest-community/eslint-plugin-jest/compare/v25.0.0-next.4...v25.0.0-next.5) (2021-09-29)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
### Bug Fixes
|
|
71
|
+
|
|
72
|
+
* **no-deprecated-functions:** remove `process.cwd` from resolve paths ([#889](https://github.com/jest-community/eslint-plugin-jest/issues/889)) ([6940488](https://github.com/jest-community/eslint-plugin-jest/commit/6940488d7b5a47577e2823e6d4385b511c5becf4))
|
|
73
|
+
* **no-identical-title:** always consider `.each` titles unique ([#910](https://github.com/jest-community/eslint-plugin-jest/issues/910)) ([a41a40e](https://github.com/jest-community/eslint-plugin-jest/commit/a41a40eafaf1db444ba940cccd2014cb0dc41be9))
|
|
74
|
+
* **valid-expect-in-promise:** support `finally` ([#914](https://github.com/jest-community/eslint-plugin-jest/issues/914)) ([9c89855](https://github.com/jest-community/eslint-plugin-jest/commit/9c89855d23534272230afe6d9e665b8e11ef3075))
|
|
75
|
+
* **valid-expect-in-promise:** support additional test functions ([#915](https://github.com/jest-community/eslint-plugin-jest/issues/915)) ([4798005](https://github.com/jest-community/eslint-plugin-jest/commit/47980058d8d1ff86ee69a376c4edd182d462d594))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
### Features
|
|
79
|
+
|
|
80
|
+
* create `prefer-expect-resolves` rule ([#822](https://github.com/jest-community/eslint-plugin-jest/issues/822)) ([2556020](https://github.com/jest-community/eslint-plugin-jest/commit/2556020a777f9daaf1d362a04e3f990415e82db8))
|
|
81
|
+
* create `prefer-to-be` rule ([#864](https://github.com/jest-community/eslint-plugin-jest/issues/864)) ([3a64aea](https://github.com/jest-community/eslint-plugin-jest/commit/3a64aea5bdc55465f1ef34f1426ae626d6c8a230))
|
|
82
|
+
* **require-top-level-describe:** support enforcing max num of describes ([#912](https://github.com/jest-community/eslint-plugin-jest/issues/912)) ([14a2d13](https://github.com/jest-community/eslint-plugin-jest/commit/14a2d1391c9f6f52509316542f45df35853c9b79))
|
|
83
|
+
* **valid-title:** allow custom matcher messages ([#913](https://github.com/jest-community/eslint-plugin-jest/issues/913)) ([ffc9392](https://github.com/jest-community/eslint-plugin-jest/commit/ffc93921348b0d4a394125f665d2bb09148ea37e))
|
|
84
|
+
|
|
1
85
|
# [25.0.0-next.4](https://github.com/jest-community/eslint-plugin-jest/compare/v25.0.0-next.3...v25.0.0-next.4) (2021-09-20)
|
|
2
86
|
|
|
3
87
|
|
|
@@ -42,6 +126,60 @@
|
|
|
42
126
|
|
|
43
127
|
* Drop support for Node 10 and 15
|
|
44
128
|
|
|
129
|
+
# [24.7.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.6.0...v24.7.0) (2021-10-10)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
### Features
|
|
133
|
+
|
|
134
|
+
* create `require-hook` rule ([#929](https://github.com/jest-community/eslint-plugin-jest/issues/929)) ([6204b31](https://github.com/jest-community/eslint-plugin-jest/commit/6204b311e849b51a0e4705015575139f590ae7a4))
|
|
135
|
+
* deprecate `prefer-to-be-null` rule ([4db9161](https://github.com/jest-community/eslint-plugin-jest/commit/4db91612e988e84ac2facbfe466331b22eeccec9))
|
|
136
|
+
* deprecate `prefer-to-be-undefined` rule ([fa08f09](https://github.com/jest-community/eslint-plugin-jest/commit/fa08f0944e89915fb215bbeff970f12459121cb8))
|
|
137
|
+
|
|
138
|
+
# [24.6.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.5.2...v24.6.0) (2021-10-09)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
### Features
|
|
142
|
+
|
|
143
|
+
* **valid-expect-in-promise:** re-implement rule ([#916](https://github.com/jest-community/eslint-plugin-jest/issues/916)) ([7a49c58](https://github.com/jest-community/eslint-plugin-jest/commit/7a49c5831e3d85a60c11e385203b8f83d98ad580))
|
|
144
|
+
|
|
145
|
+
## [24.5.2](https://github.com/jest-community/eslint-plugin-jest/compare/v24.5.1...v24.5.2) (2021-10-04)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
### Bug Fixes
|
|
149
|
+
|
|
150
|
+
* **lowercase-name:** consider skip and only prefixes for ignores ([#923](https://github.com/jest-community/eslint-plugin-jest/issues/923)) ([8716c24](https://github.com/jest-community/eslint-plugin-jest/commit/8716c24678ea7dc7c9f692b573d1ea19a67efd84))
|
|
151
|
+
|
|
152
|
+
## [24.5.1](https://github.com/jest-community/eslint-plugin-jest/compare/v24.5.0...v24.5.1) (2021-10-04)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
### Bug Fixes
|
|
156
|
+
|
|
157
|
+
* **prefer-to-be:** don't consider RegExp literals as `toBe`-able ([#922](https://github.com/jest-community/eslint-plugin-jest/issues/922)) ([99b6d42](https://github.com/jest-community/eslint-plugin-jest/commit/99b6d429e697d60645b4bc64ee4ae34d7016118c))
|
|
158
|
+
|
|
159
|
+
# [24.5.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.3...v24.5.0) (2021-09-29)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
### Bug Fixes
|
|
163
|
+
|
|
164
|
+
* **no-deprecated-functions:** remove `process.cwd` from resolve paths ([#889](https://github.com/jest-community/eslint-plugin-jest/issues/889)) ([6940488](https://github.com/jest-community/eslint-plugin-jest/commit/6940488d7b5a47577e2823e6d4385b511c5becf4))
|
|
165
|
+
* **no-identical-title:** always consider `.each` titles unique ([#910](https://github.com/jest-community/eslint-plugin-jest/issues/910)) ([a41a40e](https://github.com/jest-community/eslint-plugin-jest/commit/a41a40eafaf1db444ba940cccd2014cb0dc41be9))
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
### Features
|
|
169
|
+
|
|
170
|
+
* create `prefer-expect-resolves` rule ([#822](https://github.com/jest-community/eslint-plugin-jest/issues/822)) ([2556020](https://github.com/jest-community/eslint-plugin-jest/commit/2556020a777f9daaf1d362a04e3f990415e82db8))
|
|
171
|
+
* create `prefer-to-be` rule ([#864](https://github.com/jest-community/eslint-plugin-jest/issues/864)) ([3a64aea](https://github.com/jest-community/eslint-plugin-jest/commit/3a64aea5bdc55465f1ef34f1426ae626d6c8a230))
|
|
172
|
+
* **require-top-level-describe:** support enforcing max num of describes ([#912](https://github.com/jest-community/eslint-plugin-jest/issues/912)) ([14a2d13](https://github.com/jest-community/eslint-plugin-jest/commit/14a2d1391c9f6f52509316542f45df35853c9b79))
|
|
173
|
+
* **valid-title:** allow custom matcher messages ([#913](https://github.com/jest-community/eslint-plugin-jest/issues/913)) ([ffc9392](https://github.com/jest-community/eslint-plugin-jest/commit/ffc93921348b0d4a394125f665d2bb09148ea37e))
|
|
174
|
+
|
|
175
|
+
## [24.4.3](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.2...v24.4.3) (2021-09-28)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
### Bug Fixes
|
|
179
|
+
|
|
180
|
+
* **valid-expect-in-promise:** support `finally` ([#914](https://github.com/jest-community/eslint-plugin-jest/issues/914)) ([9c89855](https://github.com/jest-community/eslint-plugin-jest/commit/9c89855d23534272230afe6d9e665b8e11ef3075))
|
|
181
|
+
* **valid-expect-in-promise:** support additional test functions ([#915](https://github.com/jest-community/eslint-plugin-jest/issues/915)) ([4798005](https://github.com/jest-community/eslint-plugin-jest/commit/47980058d8d1ff86ee69a376c4edd182d462d594))
|
|
182
|
+
|
|
45
183
|
## [24.4.2](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.1...v24.4.2) (2021-09-17)
|
|
46
184
|
|
|
47
185
|
|
package/README.md
CHANGED
|
@@ -59,23 +59,43 @@ doing:
|
|
|
59
59
|
This is included in all configs shared by this plugin, so can be omitted if
|
|
60
60
|
extending them.
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
depending on the version of `jest` being used.
|
|
62
|
+
### Jest `version` setting
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
64
|
+
The behaviour of some rules (specifically [`no-deprecated-functions`][]) change
|
|
65
|
+
depending on the version of Jest being used.
|
|
66
|
+
|
|
67
|
+
By default, this plugin will attempt to determine to locate Jest using
|
|
68
|
+
`require.resolve`, meaning it will start looking in the closest `node_modules`
|
|
69
|
+
folder to the file being linted and work its way up.
|
|
70
|
+
|
|
71
|
+
Since we cache the automatically determined version, if you're linting
|
|
72
|
+
sub-folders that have different versions of Jest, you may find that the wrong
|
|
73
|
+
version of Jest is considered when linting. You can work around this by
|
|
74
|
+
providing the Jest version explicitly in nested ESLint configs:
|
|
68
75
|
|
|
69
76
|
```json
|
|
70
77
|
{
|
|
71
78
|
"settings": {
|
|
72
79
|
"jest": {
|
|
73
|
-
"version":
|
|
80
|
+
"version": 27
|
|
74
81
|
}
|
|
75
82
|
}
|
|
76
83
|
}
|
|
77
84
|
```
|
|
78
85
|
|
|
86
|
+
To avoid hard-coding a number, you can also fetch it from the installed version
|
|
87
|
+
of Jest if you use a JavaScript config file such as `.eslintrc.js`:
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
module.exports = {
|
|
91
|
+
settings: {
|
|
92
|
+
jest: {
|
|
93
|
+
version: require('jest/package.json').version,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
79
99
|
## Shareable configurations
|
|
80
100
|
|
|
81
101
|
### Recommended
|
|
@@ -130,49 +150,50 @@ installations requiring long-term consistency.
|
|
|
130
150
|
|
|
131
151
|
<!-- begin base rules list -->
|
|
132
152
|
|
|
133
|
-
| Rule | Description
|
|
134
|
-
| ---------------------------------------------------------------------------- |
|
|
135
|
-
| [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages
|
|
136
|
-
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body
|
|
137
|
-
| [
|
|
138
|
-
| [
|
|
139
|
-
| [no-
|
|
140
|
-
| [no-
|
|
141
|
-
| [no-
|
|
142
|
-
| [no-
|
|
143
|
-
| [no-
|
|
144
|
-
| [no-
|
|
145
|
-
| [no-
|
|
146
|
-
| [no-
|
|
147
|
-
| [no-
|
|
148
|
-
| [no-
|
|
149
|
-
| [no-
|
|
150
|
-
| [no-
|
|
151
|
-
| [no-
|
|
152
|
-
| [no-
|
|
153
|
-
| [no-
|
|
154
|
-
| [no-
|
|
155
|
-
| [no-
|
|
156
|
-
| [no-
|
|
157
|
-
| [no-
|
|
158
|
-
| [no-test-
|
|
159
|
-
| [
|
|
160
|
-
| [prefer-
|
|
161
|
-
| [prefer-expect-
|
|
162
|
-
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases
|
|
163
|
-
| [prefer-
|
|
164
|
-
| [prefer-
|
|
165
|
-
| [prefer-
|
|
166
|
-
| [prefer-to-be
|
|
167
|
-
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()`
|
|
168
|
-
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()`
|
|
169
|
-
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo`
|
|
170
|
-
| [require-
|
|
171
|
-
| [require-
|
|
172
|
-
| [
|
|
173
|
-
| [valid-
|
|
174
|
-
| [valid-expect
|
|
175
|
-
| [valid-
|
|
153
|
+
| Rule | Description | Configurations | Fixable |
|
|
154
|
+
| ---------------------------------------------------------------------------- | ------------------------------------------------------------------- | ---------------- | ------------ |
|
|
155
|
+
| [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
|
|
156
|
+
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
|
|
157
|
+
| [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | |
|
|
158
|
+
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
|
|
159
|
+
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
|
|
160
|
+
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
|
|
161
|
+
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
|
|
162
|
+
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
|
|
163
|
+
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
|
|
164
|
+
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
|
|
165
|
+
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
|
|
166
|
+
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
|
|
167
|
+
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
|
|
168
|
+
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
|
|
169
|
+
| [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
|
|
170
|
+
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
|
|
171
|
+
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
|
|
172
|
+
| [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
|
|
173
|
+
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
|
|
174
|
+
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
|
|
175
|
+
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
|
|
176
|
+
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
|
|
177
|
+
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
|
|
178
|
+
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
|
|
179
|
+
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
|
|
180
|
+
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
|
|
181
|
+
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
|
|
182
|
+
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
|
|
183
|
+
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | ![fixable][] |
|
|
184
|
+
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
|
|
185
|
+
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
|
|
186
|
+
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | ![style][] | ![fixable][] |
|
|
187
|
+
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
|
|
188
|
+
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
|
|
189
|
+
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
|
|
190
|
+
| [require-hook](docs/rules/require-hook.md) | Require setup and teardown code to be within a hook | | |
|
|
191
|
+
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
|
|
192
|
+
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
|
|
193
|
+
| [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback | ![recommended][] | |
|
|
194
|
+
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
|
|
195
|
+
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Ensure promises that have expectations in their chain are valid | ![recommended][] | |
|
|
196
|
+
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |
|
|
176
197
|
|
|
177
198
|
<!-- end base rules list -->
|
|
178
199
|
|
|
@@ -226,3 +247,4 @@ https://github.com/istanbuljs/eslint-plugin-istanbul
|
|
|
226
247
|
[suggest]: https://img.shields.io/badge/-suggest-yellow.svg
|
|
227
248
|
[fixable]: https://img.shields.io/badge/-fixable-green.svg
|
|
228
249
|
[style]: https://img.shields.io/badge/-style-blue.svg
|
|
250
|
+
[`no-deprecated-functions`]: docs/rules/no-deprecated-functions.md
|
|
@@ -116,16 +116,15 @@ describe('foo', () => {
|
|
|
116
116
|
});
|
|
117
117
|
});
|
|
118
118
|
|
|
119
|
-
describe('foo2', function()
|
|
120
|
-
describe('bar2', function() {
|
|
121
|
-
it('should get something', function() {
|
|
119
|
+
describe('foo2', function () {
|
|
120
|
+
describe('bar2', function () {
|
|
121
|
+
it('should get something', function () {
|
|
122
122
|
expect(getSomething()).toBe('Something');
|
|
123
123
|
});
|
|
124
124
|
|
|
125
|
-
it('should get else', function() {
|
|
125
|
+
it('should get else', function () {
|
|
126
126
|
expect(getSomething()).toBe('Something');
|
|
127
127
|
});
|
|
128
128
|
});
|
|
129
129
|
});
|
|
130
|
-
|
|
131
130
|
```
|
|
@@ -8,9 +8,9 @@ assumed to be promises.
|
|
|
8
8
|
|
|
9
9
|
## Rule Details
|
|
10
10
|
|
|
11
|
-
Jest
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
Jest only considers a test to have failed if it throws an error, meaning if
|
|
12
|
+
calls to assertion functions like `expect` occur in conditional code such as a
|
|
13
|
+
`catch` statement, tests can end up passing but not actually test anything.
|
|
14
14
|
|
|
15
15
|
Additionally, conditionals tend to make tests more brittle and complex, as they
|
|
16
16
|
increase the amount of mental thinking needed to understand what is actually
|
|
@@ -79,3 +79,57 @@ it('throws an error', async () => {
|
|
|
79
79
|
await expect(foo).rejects.toThrow(Error);
|
|
80
80
|
});
|
|
81
81
|
```
|
|
82
|
+
|
|
83
|
+
### How to catch a thrown error for testing without violating this rule
|
|
84
|
+
|
|
85
|
+
A common situation that comes up with this rule is when wanting to test
|
|
86
|
+
properties on a thrown error, as Jest's `toThrow` matcher only checks the
|
|
87
|
+
`message` property.
|
|
88
|
+
|
|
89
|
+
Most people write something like this:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
describe('when the http request fails', () => {
|
|
93
|
+
it('includes the status code in the error', async () => {
|
|
94
|
+
try {
|
|
95
|
+
await makeRequest(url);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
expect(error).toHaveProperty('statusCode', 404);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
As stated above, the problem with this is that if `makeRequest()` doesn't throw
|
|
104
|
+
the test will still pass as if the `expect` had been called.
|
|
105
|
+
|
|
106
|
+
While you can use `expect.assertions` & `expect.hasAssertions` for these
|
|
107
|
+
situations, they only work with `expect`.
|
|
108
|
+
|
|
109
|
+
A better way to handle this situation is to introduce a wrapper to handle the
|
|
110
|
+
catching, and otherwise returns a specific "no error thrown" error if nothing is
|
|
111
|
+
thrown by the wrapped function:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
class NoErrorThrownError extends Error {}
|
|
115
|
+
|
|
116
|
+
const getError = async <TError>(call: () => unknown): Promise<TError> => {
|
|
117
|
+
try {
|
|
118
|
+
await call();
|
|
119
|
+
|
|
120
|
+
throw new NoErrorThrownError();
|
|
121
|
+
} catch (error: unknown) {
|
|
122
|
+
return error as TError;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
describe('when the http request fails', () => {
|
|
127
|
+
it('includes the status code in the error', async () => {
|
|
128
|
+
const error = await getError(async () => makeRequest(url));
|
|
129
|
+
|
|
130
|
+
// check that the returned error wasn't that no error was thrown
|
|
131
|
+
expect(error).not.toBeInstanceOf(NoErrorThrownError);
|
|
132
|
+
expect(error).toHaveProperty('statusCode', 404);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
```
|
|
@@ -6,6 +6,11 @@ either been renamed for clarity, or replaced with more powerful APIs.
|
|
|
6
6
|
While typically these deprecated functions are kept in the codebase for a number
|
|
7
7
|
of majors, eventually they are removed completely.
|
|
8
8
|
|
|
9
|
+
This rule requires knowing which version of Jest you're using - see
|
|
10
|
+
[this section of the readme](../../README.md#jest-version-setting) for details
|
|
11
|
+
on how that is obtained automatically and how you can explicitly provide a
|
|
12
|
+
version if needed.
|
|
13
|
+
|
|
9
14
|
## Rule details
|
|
10
15
|
|
|
11
16
|
This rule warns about calls to deprecated functions, and provides details on
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
When calling asynchronous code in hooks and tests, `jest` needs to know when the
|
|
4
4
|
asynchronous work is complete to progress the current run.
|
|
5
5
|
|
|
6
|
-
Originally the most common pattern to
|
|
6
|
+
Originally the most common pattern to achieve this was to use callbacks:
|
|
7
7
|
|
|
8
8
|
```js
|
|
9
9
|
test('the data is peanut butter', done => {
|
|
@@ -20,11 +20,11 @@ test('the data is peanut butter', done => {
|
|
|
20
20
|
});
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
This can be very error
|
|
23
|
+
This can be very error-prone however, as it requires careful understanding of
|
|
24
24
|
how assertions work in tests or otherwise tests won't behave as expected.
|
|
25
25
|
|
|
26
26
|
For example, if the `try/catch` was left out of the above code, the test would
|
|
27
|
-
|
|
27
|
+
time out rather than fail. Even with the `try/catch`, forgetting to pass the
|
|
28
28
|
caught error to `done` will result in `jest` believing the test has passed.
|
|
29
29
|
|
|
30
30
|
A more straightforward way to handle asynchronous code is to use Promises:
|
|
@@ -8,9 +8,9 @@ trigger this rule.
|
|
|
8
8
|
|
|
9
9
|
This rule aims to eliminate `expect` statements that will not be executed. An
|
|
10
10
|
`expect` inside of a `describe` block but outside of a `test` or `it` block or
|
|
11
|
-
outside
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
outside a `describe` will not execute and therefore will trigger this rule. It
|
|
12
|
+
is viable, however, to have an `expect` in a helper function that is called from
|
|
13
|
+
within a `test` or `it` block so `expect` statements in a function will not
|
|
14
14
|
trigger this rule.
|
|
15
15
|
|
|
16
16
|
Statements like `expect.hasAssertions()` will NOT trigger this rule since these
|
|
@@ -7,8 +7,7 @@ If you are returning Promises then you should update the test to use
|
|
|
7
7
|
|
|
8
8
|
## Rule details
|
|
9
9
|
|
|
10
|
-
This rule triggers a warning if you use a return statement inside
|
|
11
|
-
body.
|
|
10
|
+
This rule triggers a warning if you use a return statement inside a test body.
|
|
12
11
|
|
|
13
12
|
```js
|
|
14
13
|
/*eslint jest/no-test-return-statement: "error"*/
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Prefer `await expect(...).resolves` over `expect(await ...)` syntax (`prefer-expect-resolves`)
|
|
2
|
+
|
|
3
|
+
When working with promises, there are two primary ways you can test the resolved
|
|
4
|
+
value:
|
|
5
|
+
|
|
6
|
+
1. use the `resolve` modifier on `expect`
|
|
7
|
+
(`await expect(...).resolves.<matcher>` style)
|
|
8
|
+
2. `await` the promise and assert against its result
|
|
9
|
+
(`expect(await ...).<matcher>` style)
|
|
10
|
+
|
|
11
|
+
While the second style is arguably less dependent on `jest`, if the promise
|
|
12
|
+
rejects it will be treated as a general error, resulting in less predictable
|
|
13
|
+
behaviour and output from `jest`.
|
|
14
|
+
|
|
15
|
+
Additionally, favoring the first style ensures consistency with its `rejects`
|
|
16
|
+
counterpart, as there is no way of "awaiting" a rejection.
|
|
17
|
+
|
|
18
|
+
## Rule details
|
|
19
|
+
|
|
20
|
+
This rule triggers a warning if an `await` is done within an `expect`, and
|
|
21
|
+
recommends using `resolves` instead.
|
|
22
|
+
|
|
23
|
+
Examples of **incorrect** code for this rule
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
it('passes', async () => {
|
|
27
|
+
expect(await someValue()).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('is true', async () => {
|
|
31
|
+
const myPromise = Promise.resolve(true);
|
|
32
|
+
|
|
33
|
+
expect(await myPromise).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Examples of **correct** code for this rule
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
it('passes', async () => {
|
|
41
|
+
await expect(someValue()).resolves.toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('is true', async () => {
|
|
45
|
+
const myPromise = Promise.resolve(true);
|
|
46
|
+
|
|
47
|
+
await expect(myPromise).resolves.toBe(true);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('errors', async () => {
|
|
51
|
+
await expect(Promise.rejects('oh noes!')).rejects.toThrow('oh noes!');
|
|
52
|
+
});
|
|
53
|
+
```
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Suggest using `toBe()` for primitive literals (`prefer-to-be`)
|
|
2
|
+
|
|
3
|
+
When asserting against primitive literals such as numbers and strings, the
|
|
4
|
+
equality matchers all operate the same, but read slightly differently in code.
|
|
5
|
+
|
|
6
|
+
This rule recommends using the `toBe` matcher in these situations, as it forms
|
|
7
|
+
the most grammatically natural sentence. For `null`, `undefined`, and `NaN` this
|
|
8
|
+
rule recommends using their specific `toBe` matchers, as they give better error
|
|
9
|
+
messages as well.
|
|
10
|
+
|
|
11
|
+
## Rule details
|
|
12
|
+
|
|
13
|
+
This rule triggers a warning if `toEqual()` or `toStrictEqual()` are used to
|
|
14
|
+
assert a primitive literal value such as numbers, strings, and booleans.
|
|
15
|
+
|
|
16
|
+
The following patterns are considered warnings:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
expect(value).not.toEqual(5);
|
|
20
|
+
expect(getMessage()).toStrictEqual('hello world');
|
|
21
|
+
expect(loadMessage()).resolves.toEqual('hello world');
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The following pattern is not warning:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
expect(value).not.toBe(5);
|
|
28
|
+
expect(getMessage()).toBe('hello world');
|
|
29
|
+
expect(loadMessage()).resolves.toBe('hello world');
|
|
30
|
+
expect(didError).not.toBe(true);
|
|
31
|
+
|
|
32
|
+
expect(catchError()).toStrictEqual({ message: 'oh noes!' });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
For `null`, `undefined`, and `NaN`, this rule triggers a warning if `toBe` is
|
|
36
|
+
used to assert against those literal values instead of their more specific
|
|
37
|
+
`toBe` counterparts:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
expect(value).not.toBe(undefined);
|
|
41
|
+
expect(getMessage()).toBe(null);
|
|
42
|
+
expect(countMessages()).resolves.not.toBe(NaN);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The following pattern is not warning:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
expect(value).toBeDefined();
|
|
49
|
+
expect(getMessage()).toBeNull();
|
|
50
|
+
expect(countMessages()).resolves.not.toBeNaN();
|
|
51
|
+
|
|
52
|
+
expect(catchError()).toStrictEqual({ message: undefined });
|
|
53
|
+
```
|