eslint-plugin-jest 27.8.0 → 27.9.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/README.md +117 -6
- package/lib/index.js +49 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,6 +22,15 @@ yarn add --dev eslint eslint-plugin-jest
|
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
+
> [!NOTE]
|
|
26
|
+
>
|
|
27
|
+
> `eslint.config.js` is supported, though most of the plugin documentation still
|
|
28
|
+
> currently uses `.eslintrc` syntax.
|
|
29
|
+
>
|
|
30
|
+
> Refer to the
|
|
31
|
+
> [ESLint documentation on the new configuration file format](https://eslint.org/docs/latest/use/configure/configuration-files-new)
|
|
32
|
+
> for more.
|
|
33
|
+
|
|
25
34
|
Add `jest` to the plugins section of your `.eslintrc` configuration file. You
|
|
26
35
|
can omit the `eslint-plugin-` prefix:
|
|
27
36
|
|
|
@@ -85,7 +94,7 @@ test-related. This means it's generally not suitable to include them in your
|
|
|
85
94
|
top-level configuration as that applies to all files being linted which can
|
|
86
95
|
include source files.
|
|
87
96
|
|
|
88
|
-
|
|
97
|
+
For `.eslintrc` configs you can use
|
|
89
98
|
[overrides](https://eslint.org/docs/user-guide/configuring/configuration-files#how-do-overrides-work)
|
|
90
99
|
to have ESLint apply additional rules to specific files:
|
|
91
100
|
|
|
@@ -106,6 +115,30 @@ to have ESLint apply additional rules to specific files:
|
|
|
106
115
|
}
|
|
107
116
|
```
|
|
108
117
|
|
|
118
|
+
For `eslint.config.js` you can use
|
|
119
|
+
[`files` and `ignores`](https://eslint.org/docs/latest/use/configure/configuration-files-new#specifying-files-and-ignores):
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
const jest = require('eslint-plugin-jest');
|
|
123
|
+
|
|
124
|
+
module.exports = [
|
|
125
|
+
...require('@eslint/js').configs.recommended,
|
|
126
|
+
{
|
|
127
|
+
files: ['test/**'],
|
|
128
|
+
...jest.configs['flat/recommended'],
|
|
129
|
+
rules: {
|
|
130
|
+
...jest.configs['flat/recommended'],
|
|
131
|
+
'jest/prefer-expect-assertions': 'off',
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
// you can also configure jest rules in other objects, so long as some of the `files` match
|
|
135
|
+
{
|
|
136
|
+
files: ['test/**'],
|
|
137
|
+
rules: { 'jest/prefer-expect-assertions': 'off' },
|
|
138
|
+
},
|
|
139
|
+
];
|
|
140
|
+
```
|
|
141
|
+
|
|
109
142
|
### Jest `version` setting
|
|
110
143
|
|
|
111
144
|
The behaviour of some rules (specifically [`no-deprecated-functions`][]) change
|
|
@@ -145,13 +178,18 @@ module.exports = {
|
|
|
145
178
|
|
|
146
179
|
## Shareable configurations
|
|
147
180
|
|
|
181
|
+
> [!NOTE]
|
|
182
|
+
>
|
|
183
|
+
> `eslint.config.js` compatible versions of configs are available prefixed with
|
|
184
|
+
> `flat/` and may be subject to small breaking changes while ESLint v9 is being
|
|
185
|
+
> finalized.
|
|
186
|
+
|
|
148
187
|
### Recommended
|
|
149
188
|
|
|
150
189
|
This plugin exports a recommended configuration that enforces good testing
|
|
151
190
|
practices.
|
|
152
191
|
|
|
153
|
-
To enable this configuration use the `extends` property
|
|
154
|
-
config file:
|
|
192
|
+
To enable this configuration with `.eslintrc`, use the `extends` property:
|
|
155
193
|
|
|
156
194
|
```json
|
|
157
195
|
{
|
|
@@ -159,6 +197,22 @@ config file:
|
|
|
159
197
|
}
|
|
160
198
|
```
|
|
161
199
|
|
|
200
|
+
To enable this configuration with `eslint.config.js`, use
|
|
201
|
+
`jest.configs['flat/recommended']`:
|
|
202
|
+
|
|
203
|
+
```js
|
|
204
|
+
const jest = require('eslint-plugin-jest');
|
|
205
|
+
|
|
206
|
+
module.exports = [
|
|
207
|
+
{
|
|
208
|
+
files: [
|
|
209
|
+
/* glob matching your test files */
|
|
210
|
+
],
|
|
211
|
+
...jest.configs['flat/recommended'],
|
|
212
|
+
},
|
|
213
|
+
];
|
|
214
|
+
```
|
|
215
|
+
|
|
162
216
|
### Style
|
|
163
217
|
|
|
164
218
|
This plugin also exports a configuration named `style`, which adds some
|
|
@@ -174,9 +228,21 @@ config file:
|
|
|
174
228
|
}
|
|
175
229
|
```
|
|
176
230
|
|
|
177
|
-
|
|
178
|
-
[
|
|
179
|
-
|
|
231
|
+
To enable this configuration with `eslint.config.js`, use
|
|
232
|
+
`jest.configs['flat/style']`:
|
|
233
|
+
|
|
234
|
+
```js
|
|
235
|
+
const jest = require('eslint-plugin-jest');
|
|
236
|
+
|
|
237
|
+
module.exports = [
|
|
238
|
+
{
|
|
239
|
+
files: [
|
|
240
|
+
/* glob matching your test files */
|
|
241
|
+
],
|
|
242
|
+
...jest.configs['flat/style'],
|
|
243
|
+
},
|
|
244
|
+
];
|
|
245
|
+
```
|
|
180
246
|
|
|
181
247
|
### All
|
|
182
248
|
|
|
@@ -189,10 +255,55 @@ If you want to enable all rules instead of only some you can do so by adding the
|
|
|
189
255
|
}
|
|
190
256
|
```
|
|
191
257
|
|
|
258
|
+
To enable this configuration with `eslint.config.js`, use
|
|
259
|
+
`jest.configs['flat/all']`:
|
|
260
|
+
|
|
261
|
+
```js
|
|
262
|
+
const jest = require('eslint-plugin-jest');
|
|
263
|
+
|
|
264
|
+
module.exports = [
|
|
265
|
+
{
|
|
266
|
+
files: [
|
|
267
|
+
/* glob matching your test files */
|
|
268
|
+
],
|
|
269
|
+
...jest.configs['flat/all'],
|
|
270
|
+
},
|
|
271
|
+
];
|
|
272
|
+
```
|
|
273
|
+
|
|
192
274
|
While the `recommended` and `style` configurations only change in major versions
|
|
193
275
|
the `all` configuration may change in any release and is thus unsuited for
|
|
194
276
|
installations requiring long-term consistency.
|
|
195
277
|
|
|
278
|
+
## Snapshot processing
|
|
279
|
+
|
|
280
|
+
> [!NOTE]
|
|
281
|
+
>
|
|
282
|
+
> This is only relevant for `eslint.config.js`
|
|
283
|
+
|
|
284
|
+
This plugin provides a
|
|
285
|
+
[custom processor](https://eslint.org/docs/latest/extend/custom-processors) to
|
|
286
|
+
allow rules to "lint" snapshot files.
|
|
287
|
+
|
|
288
|
+
For `.eslintrc` based configs, this is automatically enabled out of the box but
|
|
289
|
+
must be opted into for `eslint.config.js` using the `flat/snapshots` config:
|
|
290
|
+
|
|
291
|
+
```js
|
|
292
|
+
const jest = require('eslint-plugin-jest');
|
|
293
|
+
|
|
294
|
+
module.exports = [
|
|
295
|
+
{
|
|
296
|
+
...jest.configs['flat/snapshots'],
|
|
297
|
+
rules: {
|
|
298
|
+
'jest/no-large-snapshots': ['error', { maxSize: 1 }],
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
];
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Unlike other configs, this includes a `files` array that matches `.snap` files
|
|
305
|
+
meaning you can use it directly
|
|
306
|
+
|
|
196
307
|
## Rules
|
|
197
308
|
|
|
198
309
|
<!-- begin auto-generated rules list -->
|
package/lib/index.js
CHANGED
|
@@ -23,35 +23,65 @@ const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];
|
|
|
23
23
|
const rules = Object.fromEntries((0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).map(rule => [rule, importDefault((0, _path.join)(rulesDir, rule))]));
|
|
24
24
|
const recommendedRules = Object.fromEntries(Object.entries(rules).filter(([, rule]) => rule.meta.docs.recommended).map(([name, rule]) => [`jest/${name}`, rule.meta.docs.recommended]));
|
|
25
25
|
const allRules = Object.fromEntries(Object.entries(rules).filter(([, rule]) => !rule.meta.deprecated).map(([name]) => [`jest/${name}`, 'error']));
|
|
26
|
-
const
|
|
27
|
-
plugins: ['jest'],
|
|
28
|
-
env: {
|
|
29
|
-
'jest/globals': true
|
|
30
|
-
},
|
|
31
|
-
rules
|
|
32
|
-
});
|
|
33
|
-
module.exports = {
|
|
26
|
+
const plugin = {
|
|
34
27
|
meta: {
|
|
35
28
|
name: _package.name,
|
|
36
29
|
version: _package.version
|
|
37
30
|
},
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
style: createConfig({
|
|
42
|
-
'jest/no-alias-methods': 'warn',
|
|
43
|
-
'jest/prefer-to-be': 'error',
|
|
44
|
-
'jest/prefer-to-contain': 'error',
|
|
45
|
-
'jest/prefer-to-have-length': 'error'
|
|
46
|
-
})
|
|
47
|
-
},
|
|
31
|
+
// ugly cast for now to keep TypeScript happy since
|
|
32
|
+
// we don't have types for flat config yet
|
|
33
|
+
configs: {},
|
|
48
34
|
environments: {
|
|
49
35
|
globals: {
|
|
50
36
|
globals: _globals.default
|
|
51
37
|
}
|
|
52
38
|
},
|
|
53
39
|
processors: {
|
|
40
|
+
snapshots: snapshotProcessor,
|
|
54
41
|
'.snap': snapshotProcessor
|
|
55
42
|
},
|
|
56
43
|
rules
|
|
57
|
-
};
|
|
44
|
+
};
|
|
45
|
+
const createRCConfig = rules => ({
|
|
46
|
+
plugins: ['jest'],
|
|
47
|
+
env: {
|
|
48
|
+
'jest/globals': true
|
|
49
|
+
},
|
|
50
|
+
rules
|
|
51
|
+
});
|
|
52
|
+
const createFlatConfig = rules => ({
|
|
53
|
+
plugins: {
|
|
54
|
+
jest: plugin
|
|
55
|
+
},
|
|
56
|
+
languageOptions: {
|
|
57
|
+
globals: _globals.default
|
|
58
|
+
},
|
|
59
|
+
rules
|
|
60
|
+
});
|
|
61
|
+
plugin.configs = {
|
|
62
|
+
all: createRCConfig(allRules),
|
|
63
|
+
recommended: createRCConfig(recommendedRules),
|
|
64
|
+
style: createRCConfig({
|
|
65
|
+
'jest/no-alias-methods': 'warn',
|
|
66
|
+
'jest/prefer-to-be': 'error',
|
|
67
|
+
'jest/prefer-to-contain': 'error',
|
|
68
|
+
'jest/prefer-to-have-length': 'error'
|
|
69
|
+
}),
|
|
70
|
+
'flat/all': createFlatConfig(allRules),
|
|
71
|
+
'flat/recommended': createFlatConfig(recommendedRules),
|
|
72
|
+
'flat/style': createFlatConfig({
|
|
73
|
+
'jest/no-alias-methods': 'warn',
|
|
74
|
+
'jest/prefer-to-be': 'error',
|
|
75
|
+
'jest/prefer-to-contain': 'error',
|
|
76
|
+
'jest/prefer-to-have-length': 'error'
|
|
77
|
+
}),
|
|
78
|
+
'flat/snapshots': {
|
|
79
|
+
// @ts-expect-error this is introduced in flat config
|
|
80
|
+
files: ['**/*.snap'],
|
|
81
|
+
plugins: {
|
|
82
|
+
jest: plugin
|
|
83
|
+
},
|
|
84
|
+
processor: 'jest/snapshots'
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
module.exports = plugin;
|