eslint-plugin-jest 23.11.0 → 23.13.2
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 +30 -0
- package/README.md +0 -3
- package/docs/rules/no-deprecated-functions.md +1 -1
- package/docs/rules/no-expect-resolves.md +18 -0
- package/docs/rules/no-truthy-falsy.md +21 -0
- package/docs/rules/prefer-inline-snapshots.md +21 -0
- package/docs/rules/valid-expect.md +21 -1
- package/lib/globals.json +1 -0
- package/lib/index.js +1 -1
- package/lib/rules/no-deprecated-functions.js +5 -5
- package/lib/rules/no-expect-resolves.js +2 -0
- package/lib/rules/no-if.js +2 -2
- package/lib/rules/no-large-snapshots.js +1 -1
- package/lib/rules/no-truthy-falsy.js +2 -0
- package/lib/rules/prefer-inline-snapshots.js +2 -0
- package/lib/rules/utils.js +2 -2
- package/lib/rules/valid-expect.js +47 -22
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
+
## [23.13.2](https://github.com/jest-community/eslint-plugin-jest/compare/v23.13.1...v23.13.2) (2020-05-26)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* add `fail` to globals ([#595](https://github.com/jest-community/eslint-plugin-jest/issues/595)) ([aadc5ec](https://github.com/jest-community/eslint-plugin-jest/commit/aadc5ec5610ec024eac4b0aa6077cc012a0ba98e))
|
|
7
|
+
|
|
8
|
+
## [23.13.1](https://github.com/jest-community/eslint-plugin-jest/compare/v23.13.0...v23.13.1) (2020-05-17)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **no-if:** use correct syntax for placeholder substitution in message ([6d1eda8](https://github.com/jest-community/eslint-plugin-jest/commit/6d1eda89ac48c93c2675dcf24a92574a20b2edb9))
|
|
14
|
+
|
|
15
|
+
# [23.13.0](https://github.com/jest-community/eslint-plugin-jest/compare/v23.12.0...v23.13.0) (2020-05-16)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* **valid-expect:** support `minArgs` & `maxArgs` options ([#584](https://github.com/jest-community/eslint-plugin-jest/issues/584)) ([9e0e2fa](https://github.com/jest-community/eslint-plugin-jest/commit/9e0e2fa966b43c1099d11b2424acb1590c241c03))
|
|
21
|
+
|
|
22
|
+
# [23.12.0](https://github.com/jest-community/eslint-plugin-jest/compare/v23.11.0...v23.12.0) (2020-05-16)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* deprecate `no-expect-resolves` rule ([b6a22e5](https://github.com/jest-community/eslint-plugin-jest/commit/b6a22e5aa98abcb57aac217c6d4583d0a3388e7b))
|
|
28
|
+
* deprecate `no-truthy-falsy` rule ([a67d92d](https://github.com/jest-community/eslint-plugin-jest/commit/a67d92d2834568122f24bf3d8455999166da95ea))
|
|
29
|
+
* deprecate `prefer-inline-snapshots` rule ([1360e9b](https://github.com/jest-community/eslint-plugin-jest/commit/1360e9b0e840f4f778a9d251371c943919f84600))
|
|
30
|
+
|
|
1
31
|
# [23.11.0](https://github.com/jest-community/eslint-plugin-jest/compare/v23.10.0...v23.11.0) (2020-05-12)
|
|
2
32
|
|
|
3
33
|
|
package/README.md
CHANGED
|
@@ -137,7 +137,6 @@ installations requiring long-term consistency.
|
|
|
137
137
|
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | | ![fixable][] |
|
|
138
138
|
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
|
|
139
139
|
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
|
|
140
|
-
| [no-expect-resolves](docs/rules/no-expect-resolves.md) | Disallow expect.resolves | | |
|
|
141
140
|
| [no-export](docs/rules/no-export.md) | Prevent exporting from test files | ![recommended][] | |
|
|
142
141
|
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![fixable][] |
|
|
143
142
|
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
|
|
@@ -152,12 +151,10 @@ installations requiring long-term consistency.
|
|
|
152
151
|
| [no-test-callback](docs/rules/no-test-callback.md) | Avoid using a callback in asynchronous tests | ![recommended][] | ![fixable][] |
|
|
153
152
|
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
|
|
154
153
|
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
|
|
155
|
-
| [no-truthy-falsy](docs/rules/no-truthy-falsy.md) | Disallow using `toBeTruthy()` & `toBeFalsy()` | | |
|
|
156
154
|
| [no-try-expect](docs/rules/no-try-expect.md) | Prefer using toThrow for exception tests | ![recommended][] | |
|
|
157
155
|
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` OR `toHaveBeenCalledWith()` | | |
|
|
158
156
|
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
|
|
159
157
|
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest to have all hooks at top level | | |
|
|
160
|
-
| [prefer-inline-snapshots](docs/rules/prefer-inline-snapshots.md) | Suggest using inline snapshots | | ![fixable][] |
|
|
161
158
|
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
|
|
162
159
|
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using toStrictEqual() | | ![fixable][] |
|
|
163
160
|
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using `toBeNull()` | ![style][] | ![fixable][] |
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Disallow use of deprecated functions (no-deprecated-functions)
|
|
1
|
+
# Disallow use of deprecated functions (`no-deprecated-functions`)
|
|
2
2
|
|
|
3
3
|
Over the years Jest has accrued some debt in the form of functions that have
|
|
4
4
|
either been renamed for clarity, or replaced with more powerful APIs.
|
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Avoid using `expect().resolves` (`no-expect-resolves`)
|
|
2
2
|
|
|
3
|
+
## Deprecated
|
|
4
|
+
|
|
5
|
+
This rule has been deprecated in favor of
|
|
6
|
+
[`no-restricted-matchers`](no-restricted-matchers.md) with the following config:
|
|
7
|
+
|
|
8
|
+
```json
|
|
9
|
+
{
|
|
10
|
+
"rules": {
|
|
11
|
+
"jest/no-restricted-matchers": [
|
|
12
|
+
"error",
|
|
13
|
+
{ "resolves": "Use `expect(await promise)` instead." }
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
3
21
|
Jest allows you to test a promise resolve value using `await expect().resolves`.
|
|
4
22
|
For consistency and readability this rule bans `expect().resolves` in favor of
|
|
5
23
|
`expect(await promise)`.
|
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Disallow using `toBeTruthy()` & `toBeFalsy()` (`no-truthy-falsy`)
|
|
2
2
|
|
|
3
|
+
## Deprecated
|
|
4
|
+
|
|
5
|
+
This rule has been deprecated in favor of
|
|
6
|
+
[`no-restricted-matchers`](no-restricted-matchers.md) with the following config:
|
|
7
|
+
|
|
8
|
+
```json
|
|
9
|
+
{
|
|
10
|
+
"rules": {
|
|
11
|
+
"jest/no-restricted-matchers": [
|
|
12
|
+
"error",
|
|
13
|
+
{
|
|
14
|
+
"toBeTruthy": "Avoid `toBeTruthy`",
|
|
15
|
+
"toBeFalsy": "Avoid `toBeFalsy`"
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
3
24
|
Tests against boolean values should assert true or false. Asserting `toBeTruthy`
|
|
4
25
|
or `toBeFalsy` matches non-boolean values as well and encourages weaker tests.
|
|
5
26
|
|
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Suggest using inline snapshots (`prefer-inline-snapshots`)
|
|
2
2
|
|
|
3
|
+
## Deprecated
|
|
4
|
+
|
|
5
|
+
This rule has been deprecated in favor of
|
|
6
|
+
[`no-restricted-matchers`](no-restricted-matchers.md) with the following config:
|
|
7
|
+
|
|
8
|
+
```json
|
|
9
|
+
{
|
|
10
|
+
"rules": {
|
|
11
|
+
"jest/no-restricted-matchers": [
|
|
12
|
+
"error",
|
|
13
|
+
{
|
|
14
|
+
"toThrowErrorMatchingSnapshot": "Use `toThrowErrorMatchingInlineSnapshot()` instead",
|
|
15
|
+
"toMatchSnapshot": "Use `toMatchInlineSnapshot()` instead"
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
3
24
|
In order to make snapshot tests more managable and reviewable
|
|
4
25
|
`toMatchInlineSnapshot()` and `toThrowErrorMatchingInlineSnapshot` should be
|
|
5
26
|
used to write the snapshots inline in the test file.
|
|
@@ -30,7 +30,7 @@ This rule is enabled by default.
|
|
|
30
30
|
|
|
31
31
|
## Options
|
|
32
32
|
|
|
33
|
-
```
|
|
33
|
+
```json5
|
|
34
34
|
{
|
|
35
35
|
type: 'object',
|
|
36
36
|
properties: {
|
|
@@ -38,6 +38,14 @@ This rule is enabled by default.
|
|
|
38
38
|
type: 'boolean',
|
|
39
39
|
default: false,
|
|
40
40
|
},
|
|
41
|
+
minArgs: {
|
|
42
|
+
type: 'number',
|
|
43
|
+
minimum: 1,
|
|
44
|
+
},
|
|
45
|
+
maxArgs: {
|
|
46
|
+
type: 'number',
|
|
47
|
+
minimum: 1,
|
|
48
|
+
},
|
|
41
49
|
},
|
|
42
50
|
additionalProperties: false,
|
|
43
51
|
}
|
|
@@ -70,6 +78,18 @@ test('test1', async () => {
|
|
|
70
78
|
test('test2', () => expect(Promise.resolve(2)).resolves.toBe(2));
|
|
71
79
|
```
|
|
72
80
|
|
|
81
|
+
### `minArgs` & `maxArgs`
|
|
82
|
+
|
|
83
|
+
Enforces the minimum and maximum number of arguments that `expect` can take, and
|
|
84
|
+
is required to take.
|
|
85
|
+
|
|
86
|
+
Both of these properties have a default value of `1`, which is the number of
|
|
87
|
+
arguments supported by vanilla `expect`.
|
|
88
|
+
|
|
89
|
+
This is useful when you're using libraries that increase the number of arguments
|
|
90
|
+
supported by `expect`, such as
|
|
91
|
+
[`jest-expect-message`](https://www.npmjs.com/package/jest-expect-message).
|
|
92
|
+
|
|
73
93
|
### Default configuration
|
|
74
94
|
|
|
75
95
|
The following patterns are considered warnings:
|
package/lib/globals.json
CHANGED
package/lib/index.js
CHANGED
|
@@ -35,7 +35,7 @@ const excludedFiles = ['__tests__', 'utils'];
|
|
|
35
35
|
const rules = (0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).reduce((acc, curr) => Object.assign(acc, {
|
|
36
36
|
[curr]: importDefault((0, _path.join)(rulesDir, curr))
|
|
37
37
|
}), {});
|
|
38
|
-
const allRules = Object.keys(rules).reduce((rules, key) => _objectSpread({}, rules, {
|
|
38
|
+
const allRules = Object.keys(rules).reduce((rules, key) => _objectSpread(_objectSpread({}, rules), {}, {
|
|
39
39
|
[`jest/${key}`]: 'error'
|
|
40
40
|
}), {});
|
|
41
41
|
module.exports = {
|
|
@@ -66,16 +66,16 @@ var _default = (0, _utils.createRule)({
|
|
|
66
66
|
|
|
67
67
|
const jestVersion = ((_ref = context.settings) === null || _ref === void 0 ? void 0 : (_ref$jest = _ref.jest) === null || _ref$jest === void 0 ? void 0 : _ref$jest.version) || detectJestVersion();
|
|
68
68
|
|
|
69
|
-
const deprecations = _objectSpread({}, jestVersion >= 15 && {
|
|
69
|
+
const deprecations = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, jestVersion >= 15 && {
|
|
70
70
|
'jest.resetModuleRegistry': 'jest.resetModules'
|
|
71
|
-
},
|
|
71
|
+
}), jestVersion >= 17 && {
|
|
72
72
|
'jest.addMatchers': 'expect.extend'
|
|
73
|
-
},
|
|
73
|
+
}), jestVersion >= 21 && {
|
|
74
74
|
'require.requireMock': 'jest.requireMock',
|
|
75
75
|
'require.requireActual': 'jest.requireActual'
|
|
76
|
-
},
|
|
76
|
+
}), jestVersion >= 22 && {
|
|
77
77
|
'jest.runTimersToTime': 'jest.advanceTimersByTime'
|
|
78
|
-
},
|
|
78
|
+
}), jestVersion >= 26 && {
|
|
79
79
|
'jest.genMockFromModule': 'jest.createMockFromModule'
|
|
80
80
|
});
|
|
81
81
|
|
package/lib/rules/no-if.js
CHANGED
|
@@ -28,7 +28,7 @@ var _default = (0, _utils.createRule)({
|
|
|
28
28
|
recommended: false
|
|
29
29
|
},
|
|
30
30
|
messages: {
|
|
31
|
-
|
|
31
|
+
conditionalInTest: 'Test should not contain {{ condition }} statements.'
|
|
32
32
|
},
|
|
33
33
|
schema: [],
|
|
34
34
|
type: 'suggestion'
|
|
@@ -49,7 +49,7 @@ var _default = (0, _utils.createRule)({
|
|
|
49
49
|
data: {
|
|
50
50
|
condition: conditionName[node.type]
|
|
51
51
|
},
|
|
52
|
-
messageId: '
|
|
52
|
+
messageId: 'conditionalInTest',
|
|
53
53
|
node
|
|
54
54
|
});
|
|
55
55
|
}
|
|
@@ -110,7 +110,7 @@ var _default = (0, _utils.createRule)({
|
|
|
110
110
|
if ('property' in node.callee && ((0, _utils.isSupportedAccessor)(node.callee.property, 'toMatchInlineSnapshot') || (0, _utils.isSupportedAccessor)(node.callee.property, 'toThrowErrorMatchingInlineSnapshot'))) {
|
|
111
111
|
var _options$inlineMaxSiz;
|
|
112
112
|
|
|
113
|
-
reportOnViolation(context, node, _objectSpread({}, options, {
|
|
113
|
+
reportOnViolation(context, node, _objectSpread(_objectSpread({}, options), {}, {
|
|
114
114
|
maxSize: (_options$inlineMaxSiz = options.inlineMaxSize) !== null && _options$inlineMaxSiz !== void 0 ? _options$inlineMaxSiz : options.maxSize
|
|
115
115
|
}));
|
|
116
116
|
}
|
|
@@ -16,6 +16,8 @@ var _default = (0, _utils.createRule)({
|
|
|
16
16
|
description: 'Disallow using `toBeTruthy()` & `toBeFalsy()`',
|
|
17
17
|
recommended: false
|
|
18
18
|
},
|
|
19
|
+
deprecated: true,
|
|
20
|
+
replacedBy: ['no-restricted-matchers'],
|
|
19
21
|
messages: {
|
|
20
22
|
avoidMatcher: 'Avoid {{ matcherName }}'
|
|
21
23
|
},
|
|
@@ -17,6 +17,8 @@ var _default = (0, _utils.createRule)({
|
|
|
17
17
|
description: 'Suggest using inline snapshots',
|
|
18
18
|
recommended: false
|
|
19
19
|
},
|
|
20
|
+
deprecated: true,
|
|
21
|
+
replacedBy: ['no-restricted-matchers'],
|
|
20
22
|
messages: {
|
|
21
23
|
toMatch: 'Use toMatchInlineSnapshot() instead',
|
|
22
24
|
toMatchError: 'Use toThrowErrorMatchingInlineSnapshot() instead'
|
package/lib/rules/utils.js
CHANGED
|
@@ -231,7 +231,7 @@ const parseExpectMember = expectMember => ({
|
|
|
231
231
|
node: expectMember
|
|
232
232
|
});
|
|
233
233
|
|
|
234
|
-
const reparseAsMatcher = parsedMember => _objectSpread({}, parsedMember, {
|
|
234
|
+
const reparseAsMatcher = parsedMember => _objectSpread(_objectSpread({}, parsedMember), {}, {
|
|
235
235
|
/**
|
|
236
236
|
* The arguments being passed to this `Matcher`, if any.
|
|
237
237
|
*
|
|
@@ -265,7 +265,7 @@ const reparseMemberAsModifier = parsedMember => {
|
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
const negation = parsedMember.node.parent && isExpectMember(parsedMember.node.parent, ModifierName.not) ? parsedMember.node.parent : undefined;
|
|
268
|
-
return _objectSpread({}, parsedMember, {
|
|
268
|
+
return _objectSpread(_objectSpread({}, parsedMember), {}, {
|
|
269
269
|
negation
|
|
270
270
|
});
|
|
271
271
|
};
|
|
@@ -64,7 +64,8 @@ var _default = (0, _utils.createRule)({
|
|
|
64
64
|
recommended: 'error'
|
|
65
65
|
},
|
|
66
66
|
messages: {
|
|
67
|
-
|
|
67
|
+
tooManyArgs: 'Expect takes at most {{ amount }} argument{{ s }}.',
|
|
68
|
+
notEnoughArgs: 'Expect requires at least {{ amount }} argument{{ s }}.',
|
|
68
69
|
modifierUnknown: 'Expect has no modifier named "{{ modifierName }}".',
|
|
69
70
|
matcherNotFound: 'Expect must have a corresponding matcher call.',
|
|
70
71
|
matcherNotCalled: 'Matchers must be called to assert.',
|
|
@@ -78,17 +79,29 @@ var _default = (0, _utils.createRule)({
|
|
|
78
79
|
alwaysAwait: {
|
|
79
80
|
type: 'boolean',
|
|
80
81
|
default: false
|
|
82
|
+
},
|
|
83
|
+
minArgs: {
|
|
84
|
+
type: 'number',
|
|
85
|
+
minimum: 1
|
|
86
|
+
},
|
|
87
|
+
maxArgs: {
|
|
88
|
+
type: 'number',
|
|
89
|
+
minimum: 1
|
|
81
90
|
}
|
|
82
91
|
},
|
|
83
92
|
additionalProperties: false
|
|
84
93
|
}]
|
|
85
94
|
},
|
|
86
95
|
defaultOptions: [{
|
|
87
|
-
alwaysAwait: false
|
|
96
|
+
alwaysAwait: false,
|
|
97
|
+
minArgs: 1,
|
|
98
|
+
maxArgs: 1
|
|
88
99
|
}],
|
|
89
100
|
|
|
90
101
|
create(context, [{
|
|
91
|
-
alwaysAwait
|
|
102
|
+
alwaysAwait,
|
|
103
|
+
minArgs = 1,
|
|
104
|
+
maxArgs = 1
|
|
92
105
|
}]) {
|
|
93
106
|
// Context state
|
|
94
107
|
const arrayExceptions = new Set();
|
|
@@ -117,9 +130,9 @@ var _default = (0, _utils.createRule)({
|
|
|
117
130
|
matcher
|
|
118
131
|
} = (0, _utils.parseExpectCall)(node);
|
|
119
132
|
|
|
120
|
-
if (expect.arguments.length
|
|
133
|
+
if (expect.arguments.length < minArgs) {
|
|
121
134
|
const expectLength = (0, _utils.getAccessorValue)(expect.callee).length;
|
|
122
|
-
|
|
135
|
+
const loc = {
|
|
123
136
|
start: {
|
|
124
137
|
column: node.loc.start.column + expectLength,
|
|
125
138
|
line: node.loc.start.line
|
|
@@ -129,25 +142,37 @@ var _default = (0, _utils.createRule)({
|
|
|
129
142
|
line: node.loc.start.line
|
|
130
143
|
}
|
|
131
144
|
};
|
|
145
|
+
context.report({
|
|
146
|
+
messageId: 'notEnoughArgs',
|
|
147
|
+
data: {
|
|
148
|
+
amount: minArgs,
|
|
149
|
+
s: minArgs === 1 ? '' : 's'
|
|
150
|
+
},
|
|
151
|
+
node,
|
|
152
|
+
loc
|
|
153
|
+
});
|
|
154
|
+
}
|
|
132
155
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
156
|
+
if (expect.arguments.length > maxArgs) {
|
|
157
|
+
const {
|
|
158
|
+
start
|
|
159
|
+
} = expect.arguments[maxArgs].loc;
|
|
160
|
+
const {
|
|
161
|
+
end
|
|
162
|
+
} = expect.arguments[node.arguments.length - 1].loc;
|
|
163
|
+
const loc = {
|
|
164
|
+
start,
|
|
165
|
+
end: {
|
|
166
|
+
column: end.column - 1,
|
|
167
|
+
line: end.line
|
|
168
|
+
}
|
|
169
|
+
};
|
|
149
170
|
context.report({
|
|
150
|
-
messageId: '
|
|
171
|
+
messageId: 'tooManyArgs',
|
|
172
|
+
data: {
|
|
173
|
+
amount: maxArgs,
|
|
174
|
+
s: maxArgs === 1 ? '' : 's'
|
|
175
|
+
},
|
|
151
176
|
node,
|
|
152
177
|
loc
|
|
153
178
|
});
|