eslint-plugin-jest 27.9.0 β 28.0.0-next.1
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 +59 -61
- package/docs/rules/no-done-callback.md +1 -1
- package/docs/rules/no-focused-tests.md +1 -1
- package/docs/rules/prefer-equality-matcher.md +1 -1
- package/docs/rules/prefer-expect-assertions.md +1 -1
- package/docs/rules/prefer-strict-equal.md +1 -1
- package/docs/rules/unbound-method.md +3 -3
- package/lib/index.js +29 -13
- package/package.json +6 -6
- package/docs/rules/no-if.md +0 -58
- package/lib/rules/no-if.js +0 -86
package/README.md
CHANGED
|
@@ -127,7 +127,7 @@ module.exports = [
|
|
|
127
127
|
files: ['test/**'],
|
|
128
128
|
...jest.configs['flat/recommended'],
|
|
129
129
|
rules: {
|
|
130
|
-
...jest.configs['flat/recommended'],
|
|
130
|
+
...jest.configs['flat/recommended'].rules,
|
|
131
131
|
'jest/prefer-expect-assertions': 'off',
|
|
132
132
|
},
|
|
133
133
|
},
|
|
@@ -318,69 +318,67 @@ set to warn in.\
|
|
|
318
318
|
π¨ Set in the `style` [configuration](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).\
|
|
319
319
|
π§ Automatically fixable by the
|
|
320
320
|
[`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
|
|
321
|
-
π‘ Manually fixable by [editor suggestions](https://eslint.org/docs/
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
|
325
|
-
|
|
|
326
|
-
| [
|
|
327
|
-
| [
|
|
328
|
-
| [max-
|
|
329
|
-
| [
|
|
330
|
-
| [no-
|
|
331
|
-
| [no-
|
|
332
|
-
| [no-conditional-
|
|
333
|
-
| [no-
|
|
334
|
-
| [no-
|
|
335
|
-
| [no-
|
|
336
|
-
| [no-
|
|
337
|
-
| [no-
|
|
338
|
-
| [no-
|
|
339
|
-
| [no-
|
|
340
|
-
| [no-
|
|
341
|
-
| [no-
|
|
342
|
-
| [no-
|
|
343
|
-
| [no-
|
|
344
|
-
| [no-
|
|
345
|
-
| [no-
|
|
346
|
-
| [no-
|
|
347
|
-
| [no-
|
|
348
|
-
| [no-
|
|
349
|
-
| [no-
|
|
350
|
-
| [no-
|
|
351
|
-
| [no-
|
|
352
|
-
| [
|
|
353
|
-
| [
|
|
354
|
-
| [prefer-
|
|
355
|
-
| [prefer-
|
|
356
|
-
| [prefer-
|
|
357
|
-
| [prefer-
|
|
358
|
-
| [prefer-
|
|
359
|
-
| [prefer-
|
|
360
|
-
| [prefer-
|
|
361
|
-
| [prefer-
|
|
362
|
-
| [prefer-
|
|
363
|
-
| [prefer-
|
|
364
|
-
| [prefer-
|
|
365
|
-
| [prefer-
|
|
366
|
-
| [prefer-
|
|
367
|
-
| [prefer-to-
|
|
368
|
-
| [prefer-
|
|
369
|
-
| [
|
|
370
|
-
| [
|
|
371
|
-
| [require-
|
|
372
|
-
| [
|
|
373
|
-
| [
|
|
374
|
-
| [valid-
|
|
375
|
-
| [valid-
|
|
376
|
-
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Require promises that have expectations in their chain to be valid | β
| | | | |
|
|
377
|
-
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | β
| | π§ | | |
|
|
321
|
+
π‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
322
|
+
|
|
323
|
+
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π§ | π‘ |
|
|
324
|
+
| :--------------------------------------------------------------------------- | :------------------------------------------------------------------------ | :-- | :-- | :-- | :-- |
|
|
325
|
+
| [consistent-test-it](docs/rules/consistent-test-it.md) | Enforce `test` and `it` usage conventions | | | π§ | |
|
|
326
|
+
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | | β
| | |
|
|
327
|
+
| [max-expects](docs/rules/max-expects.md) | Enforces a maximum number assertion calls in a test body | | | | |
|
|
328
|
+
| [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | | | |
|
|
329
|
+
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | β
| π¨ | π§ | |
|
|
330
|
+
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | | β
| | |
|
|
331
|
+
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Disallow calling `expect` conditionally | β
| | | |
|
|
332
|
+
| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | | | |
|
|
333
|
+
| [no-confusing-set-timeout](docs/rules/no-confusing-set-timeout.md) | Disallow confusing usages of jest.setTimeout | | | | |
|
|
334
|
+
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | β
| | π§ | |
|
|
335
|
+
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | | β
| | |
|
|
336
|
+
| [no-done-callback](docs/rules/no-done-callback.md) | Disallow using a callback in asynchronous tests and hooks | β
| | | π‘ |
|
|
337
|
+
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | | | |
|
|
338
|
+
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | β
| | | |
|
|
339
|
+
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | β
| | | π‘ |
|
|
340
|
+
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | | | |
|
|
341
|
+
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | β
| | | |
|
|
342
|
+
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | β
| | | |
|
|
343
|
+
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | β
| | π§ | |
|
|
344
|
+
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | Disallow large snapshots | | | | |
|
|
345
|
+
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | β
| | | |
|
|
346
|
+
| [no-restricted-jest-methods](docs/rules/no-restricted-jest-methods.md) | Disallow specific `jest.` methods | | | | |
|
|
347
|
+
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | | | |
|
|
348
|
+
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | β
| | | |
|
|
349
|
+
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Require using `.only` and `.skip` over `f` and `x` | β
| | π§ | |
|
|
350
|
+
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | | | |
|
|
351
|
+
| [no-untyped-mock-factory](docs/rules/no-untyped-mock-factory.md) | Disallow using `jest.mock()` factories without an explicit type parameter | | | π§ | |
|
|
352
|
+
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | | | |
|
|
353
|
+
| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | | π§ | |
|
|
354
|
+
| [prefer-each](docs/rules/prefer-each.md) | Prefer using `.each` rather than manual loops | | | | |
|
|
355
|
+
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in equality matchers | | | | π‘ |
|
|
356
|
+
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | | | π‘ |
|
|
357
|
+
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | | π§ | |
|
|
358
|
+
| [prefer-hooks-in-order](docs/rules/prefer-hooks-in-order.md) | Prefer having hooks in a consistent order | | | | |
|
|
359
|
+
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | | | |
|
|
360
|
+
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | | π§ | |
|
|
361
|
+
| [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | | π§ | |
|
|
362
|
+
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | | | |
|
|
363
|
+
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | | π§ | |
|
|
364
|
+
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | | | π‘ |
|
|
365
|
+
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | π¨ | | π§ | |
|
|
366
|
+
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | π¨ | | π§ | |
|
|
367
|
+
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | π¨ | | π§ | |
|
|
368
|
+
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | | π§ | |
|
|
369
|
+
| [require-hook](docs/rules/require-hook.md) | Require setup and teardown code to be within a hook | | | | |
|
|
370
|
+
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | | | |
|
|
371
|
+
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | | | |
|
|
372
|
+
| [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback | β
| | | |
|
|
373
|
+
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | β
| | | |
|
|
374
|
+
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Require promises that have expectations in their chain to be valid | β
| | | |
|
|
375
|
+
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | β
| | π§ | |
|
|
378
376
|
|
|
379
377
|
### Requires Type Checking
|
|
380
378
|
|
|
381
|
-
| NameΒ Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π§ | π‘ |
|
|
382
|
-
| :--------------------------------------------- | :----------------------------------------------------------- | :-- | :-- | :-- | :-- |
|
|
383
|
-
| [unbound-method](docs/rules/unbound-method.md) | Enforce unbound methods are called with their expected scope | | | | |
|
|
379
|
+
| NameΒ Β Β Β Β Β Β Β Β Β | Description | πΌ | β οΈ | π§ | π‘ |
|
|
380
|
+
| :--------------------------------------------- | :----------------------------------------------------------- | :-- | :-- | :-- | :-- |
|
|
381
|
+
| [unbound-method](docs/rules/unbound-method.md) | Enforce unbound methods are called with their expected scope | | | | |
|
|
384
382
|
|
|
385
383
|
<!-- end auto-generated rules list -->
|
|
386
384
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
5
5
|
|
|
6
6
|
π‘ This rule is manually fixable by
|
|
7
|
-
[editor suggestions](https://eslint.org/docs/
|
|
7
|
+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
8
8
|
|
|
9
9
|
<!-- end auto-generated rule header -->
|
|
10
10
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
5
5
|
|
|
6
6
|
π‘ This rule is manually fixable by
|
|
7
|
-
[editor suggestions](https://eslint.org/docs/
|
|
7
|
+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
8
8
|
|
|
9
9
|
<!-- end auto-generated rule header -->
|
|
10
10
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Suggest using the built-in equality matchers (`prefer-equality-matcher`)
|
|
2
2
|
|
|
3
3
|
π‘ This rule is manually fixable by
|
|
4
|
-
[editor suggestions](https://eslint.org/docs/
|
|
4
|
+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
5
5
|
|
|
6
6
|
<!-- end auto-generated rule header -->
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Suggest using `expect.assertions()` OR `expect.hasAssertions()` (`prefer-expect-assertions`)
|
|
2
2
|
|
|
3
3
|
π‘ This rule is manually fixable by
|
|
4
|
-
[editor suggestions](https://eslint.org/docs/
|
|
4
|
+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
5
5
|
|
|
6
6
|
<!-- end auto-generated rule header -->
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Suggest using `toStrictEqual()` (`prefer-strict-equal`)
|
|
2
2
|
|
|
3
3
|
π‘ This rule is manually fixable by
|
|
4
|
-
[editor suggestions](https://eslint.org/docs/
|
|
4
|
+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
5
5
|
|
|
6
6
|
<!-- end auto-generated rule header -->
|
|
7
7
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# Enforce unbound methods are called with their expected scope (`unbound-method`)
|
|
2
2
|
|
|
3
|
-
π This rule requires
|
|
3
|
+
π This rule requires
|
|
4
|
+
[type information](https://typescript-eslint.io/linting/typed-linting).
|
|
4
5
|
|
|
5
6
|
<!-- end auto-generated rule header -->
|
|
6
7
|
|
|
@@ -55,5 +56,4 @@ See [`@typescript-eslint/unbound-method`][original-rule] options (e.g.
|
|
|
55
56
|
|
|
56
57
|
<sup>Taken with β€οΈ [from `@typescript-eslint` core][original-rule]</sup>
|
|
57
58
|
|
|
58
|
-
[original-rule]:
|
|
59
|
-
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/unbound-method.md
|
|
59
|
+
[original-rule]: https://typescript-eslint.io/rules/unbound-method
|
package/lib/index.js
CHANGED
|
@@ -21,7 +21,33 @@ interopRequireDefault(require(moduleName)).default;
|
|
|
21
21
|
const rulesDir = (0, _path.join)(__dirname, 'rules');
|
|
22
22
|
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
|
-
const recommendedRules =
|
|
24
|
+
const recommendedRules = {
|
|
25
|
+
'jest/expect-expect': 'warn',
|
|
26
|
+
'jest/no-alias-methods': 'error',
|
|
27
|
+
'jest/no-commented-out-tests': 'warn',
|
|
28
|
+
'jest/no-conditional-expect': 'error',
|
|
29
|
+
'jest/no-deprecated-functions': 'error',
|
|
30
|
+
'jest/no-disabled-tests': 'warn',
|
|
31
|
+
'jest/no-done-callback': 'error',
|
|
32
|
+
'jest/no-export': 'error',
|
|
33
|
+
'jest/no-focused-tests': 'error',
|
|
34
|
+
'jest/no-identical-title': 'error',
|
|
35
|
+
'jest/no-interpolation-in-snapshots': 'error',
|
|
36
|
+
'jest/no-jasmine-globals': 'error',
|
|
37
|
+
'jest/no-mocks-import': 'error',
|
|
38
|
+
'jest/no-standalone-expect': 'error',
|
|
39
|
+
'jest/no-test-prefixes': 'error',
|
|
40
|
+
'jest/valid-describe-callback': 'error',
|
|
41
|
+
'jest/valid-expect': 'error',
|
|
42
|
+
'jest/valid-expect-in-promise': 'error',
|
|
43
|
+
'jest/valid-title': 'error'
|
|
44
|
+
};
|
|
45
|
+
const styleRules = {
|
|
46
|
+
'jest/no-alias-methods': 'warn',
|
|
47
|
+
'jest/prefer-to-be': 'error',
|
|
48
|
+
'jest/prefer-to-contain': 'error',
|
|
49
|
+
'jest/prefer-to-have-length': 'error'
|
|
50
|
+
};
|
|
25
51
|
const allRules = Object.fromEntries(Object.entries(rules).filter(([, rule]) => !rule.meta.deprecated).map(([name]) => [`jest/${name}`, 'error']));
|
|
26
52
|
const plugin = {
|
|
27
53
|
meta: {
|
|
@@ -61,20 +87,10 @@ const createFlatConfig = rules => ({
|
|
|
61
87
|
plugin.configs = {
|
|
62
88
|
all: createRCConfig(allRules),
|
|
63
89
|
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
|
-
}),
|
|
90
|
+
style: createRCConfig(styleRules),
|
|
70
91
|
'flat/all': createFlatConfig(allRules),
|
|
71
92
|
'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
|
-
}),
|
|
93
|
+
'flat/style': createFlatConfig(styleRules),
|
|
78
94
|
'flat/snapshots': {
|
|
79
95
|
// @ts-expect-error this is introduced in flat config
|
|
80
96
|
files: ['**/*.snap'],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "28.0.0-next.1",
|
|
4
4
|
"description": "ESLint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "babel --extensions .js,.ts src --out-dir lib --copy-files && rimraf --glob lib/__tests__ 'lib/**/__tests__'",
|
|
24
|
-
"_postinstall": "is-ci || husky
|
|
24
|
+
"_postinstall": "is-ci || husky",
|
|
25
25
|
"lint": "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts",
|
|
26
26
|
"prepack": "rimraf lib && yarn build",
|
|
27
27
|
"prepublishOnly": "pinst --disable",
|
|
@@ -126,7 +126,7 @@
|
|
|
126
126
|
"eslint-plugin-prettier": "^5.0.0",
|
|
127
127
|
"eslint-remote-tester": "^3.0.0",
|
|
128
128
|
"eslint-remote-tester-repositories": "~1.0.0",
|
|
129
|
-
"husky": "^
|
|
129
|
+
"husky": "^9.0.1",
|
|
130
130
|
"is-ci": "^3.0.0",
|
|
131
131
|
"jest": "^29.0.0",
|
|
132
132
|
"jest-runner-eslint": "^2.0.0",
|
|
@@ -135,7 +135,7 @@
|
|
|
135
135
|
"pinst": "^3.0.0",
|
|
136
136
|
"prettier": "^3.0.0",
|
|
137
137
|
"rimraf": "^5.0.0",
|
|
138
|
-
"semantic-release": "^
|
|
138
|
+
"semantic-release": "^23.0.0",
|
|
139
139
|
"semver": "^7.3.5",
|
|
140
140
|
"strip-ansi": "^6.0.0",
|
|
141
141
|
"ts-node": "^10.2.1",
|
|
@@ -154,7 +154,7 @@
|
|
|
154
154
|
"optional": true
|
|
155
155
|
}
|
|
156
156
|
},
|
|
157
|
-
"packageManager": "yarn@3.8.
|
|
157
|
+
"packageManager": "yarn@3.8.1",
|
|
158
158
|
"engines": {
|
|
159
159
|
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
|
160
160
|
},
|
|
@@ -162,6 +162,6 @@
|
|
|
162
162
|
"provenance": true
|
|
163
163
|
},
|
|
164
164
|
"resolutions": {
|
|
165
|
-
"typescript": "
|
|
165
|
+
"@typescript-eslint/typescript-estree@5.62.0": "patch:@typescript-eslint/typescript-estree@npm:^5.62.0#./.yarn/patches/@typescript-eslint-typescript-estree-npm-5.62.0-5d1ea132a9.patch"
|
|
166
166
|
}
|
|
167
167
|
}
|
package/docs/rules/no-if.md
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# Disallow conditional logic (`no-if`)
|
|
2
|
-
|
|
3
|
-
β This rule is deprecated. It was replaced by
|
|
4
|
-
[`jest/no-conditional-in-test`](no-conditional-in-test.md).
|
|
5
|
-
|
|
6
|
-
<!-- end auto-generated rule header -->
|
|
7
|
-
|
|
8
|
-
Conditional logic in tests is usually an indication that a test is attempting to
|
|
9
|
-
cover too much, and not testing the logic it intends to. Each branch of code
|
|
10
|
-
executing within an if statement will usually be better served by a test devoted
|
|
11
|
-
to it.
|
|
12
|
-
|
|
13
|
-
Conditionals are often used to satisfy the typescript type checker. In these
|
|
14
|
-
cases, using the non-null assertion operator (!) would be best.
|
|
15
|
-
|
|
16
|
-
## Rule details
|
|
17
|
-
|
|
18
|
-
This rule prevents the use of if/ else statements and conditional (ternary)
|
|
19
|
-
operations in tests.
|
|
20
|
-
|
|
21
|
-
The following patterns are considered warnings:
|
|
22
|
-
|
|
23
|
-
```js
|
|
24
|
-
it('foo', () => {
|
|
25
|
-
if ('bar') {
|
|
26
|
-
// an if statement here is invalid
|
|
27
|
-
// you are probably testing too much
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('foo', () => {
|
|
32
|
-
const bar = foo ? 'bar' : null;
|
|
33
|
-
});
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
These patterns would not be considered warnings:
|
|
37
|
-
|
|
38
|
-
```js
|
|
39
|
-
it('foo', () => {
|
|
40
|
-
// only test the 'foo' case
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('bar', () => {
|
|
44
|
-
// test the 'bar' case separately
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('foo', () => {
|
|
48
|
-
function foo(bar) {
|
|
49
|
-
// nested functions are valid
|
|
50
|
-
return foo ? bar : null;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## When Not To Use It
|
|
56
|
-
|
|
57
|
-
If you do not wish to prevent the use of if statements in tests, you can safely
|
|
58
|
-
disable this rule.
|
package/lib/rules/no-if.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
var _utils = require("@typescript-eslint/utils");
|
|
8
|
-
var _utils2 = require("./utils");
|
|
9
|
-
const testCaseNames = new Set([...Object.keys(_utils2.TestCaseName), 'it.only', 'it.only', 'it.skip', 'it.skip', 'test.only', 'test.only', 'test.skip', 'test.skip', 'fit.concurrent']);
|
|
10
|
-
const isTestFunctionExpression = node => node.parent !== undefined && node.parent.type === _utils.AST_NODE_TYPES.CallExpression && testCaseNames.has((0, _utils2.getNodeName)(node.parent.callee));
|
|
11
|
-
const conditionName = {
|
|
12
|
-
[_utils.AST_NODE_TYPES.ConditionalExpression]: 'conditional',
|
|
13
|
-
[_utils.AST_NODE_TYPES.SwitchStatement]: 'switch',
|
|
14
|
-
[_utils.AST_NODE_TYPES.IfStatement]: 'if'
|
|
15
|
-
};
|
|
16
|
-
var _default = exports.default = (0, _utils2.createRule)({
|
|
17
|
-
name: __filename,
|
|
18
|
-
meta: {
|
|
19
|
-
docs: {
|
|
20
|
-
description: 'Disallow conditional logic',
|
|
21
|
-
category: 'Best Practices',
|
|
22
|
-
recommended: false
|
|
23
|
-
},
|
|
24
|
-
messages: {
|
|
25
|
-
conditionalInTest: 'Test should not contain {{ condition }} statements'
|
|
26
|
-
},
|
|
27
|
-
deprecated: true,
|
|
28
|
-
replacedBy: ['no-conditional-in-test'],
|
|
29
|
-
schema: [],
|
|
30
|
-
type: 'suggestion'
|
|
31
|
-
},
|
|
32
|
-
defaultOptions: [],
|
|
33
|
-
create(context) {
|
|
34
|
-
const stack = [];
|
|
35
|
-
function validate(node) {
|
|
36
|
-
const lastElementInStack = stack[stack.length - 1];
|
|
37
|
-
if (stack.length === 0 || !lastElementInStack) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
context.report({
|
|
41
|
-
data: {
|
|
42
|
-
condition: conditionName[node.type]
|
|
43
|
-
},
|
|
44
|
-
messageId: 'conditionalInTest',
|
|
45
|
-
node
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
CallExpression(node) {
|
|
50
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
51
|
-
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'test') {
|
|
52
|
-
stack.push(true);
|
|
53
|
-
if (jestFnCall.members.some(s => (0, _utils2.getAccessorValue)(s) === 'each')) {
|
|
54
|
-
stack.push(true);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
FunctionExpression(node) {
|
|
59
|
-
stack.push(isTestFunctionExpression(node));
|
|
60
|
-
},
|
|
61
|
-
FunctionDeclaration(node) {
|
|
62
|
-
const declaredVariables = (0, _utils2.getDeclaredVariables)(context, node);
|
|
63
|
-
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
|
|
64
|
-
stack.push(testCallExpressions.length > 0);
|
|
65
|
-
},
|
|
66
|
-
ArrowFunctionExpression(node) {
|
|
67
|
-
stack.push(isTestFunctionExpression(node));
|
|
68
|
-
},
|
|
69
|
-
IfStatement: validate,
|
|
70
|
-
SwitchStatement: validate,
|
|
71
|
-
ConditionalExpression: validate,
|
|
72
|
-
'CallExpression:exit'() {
|
|
73
|
-
stack.pop();
|
|
74
|
-
},
|
|
75
|
-
'FunctionExpression:exit'() {
|
|
76
|
-
stack.pop();
|
|
77
|
-
},
|
|
78
|
-
'FunctionDeclaration:exit'() {
|
|
79
|
-
stack.pop();
|
|
80
|
-
},
|
|
81
|
-
'ArrowFunctionExpression:exit'() {
|
|
82
|
-
stack.pop();
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
});
|