eslint-plugin-jest 27.0.4 → 27.1.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
CHANGED
|
@@ -220,6 +220,7 @@ installations requiring long-term consistency.
|
|
|
220
220
|
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
|
|
221
221
|
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
|
|
222
222
|
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
|
|
223
|
+
| [no-restricted-jest-methods](docs/rules/no-restricted-jest-methods.md) | Disallow specific `jest.` methods | | |
|
|
223
224
|
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
|
|
224
225
|
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
|
|
225
226
|
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Disallow specific `jest.` methods (`no-restricted-jest-methods`)
|
|
2
|
+
|
|
3
|
+
💼 This rule is enabled in the following
|
|
4
|
+
[configs](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations):
|
|
5
|
+
`all`.
|
|
6
|
+
|
|
7
|
+
<!-- end rule header -->
|
|
8
|
+
|
|
9
|
+
You may wish to restrict the use of specific `jest` methods.
|
|
10
|
+
|
|
11
|
+
## Rule details
|
|
12
|
+
|
|
13
|
+
This rule checks for the usage of specific methods on the `jest` object, which
|
|
14
|
+
can be used to disallow certain patterns such as spies and mocks.
|
|
15
|
+
|
|
16
|
+
## Options
|
|
17
|
+
|
|
18
|
+
Restrictions are expressed in the form of a map, with the value being either a
|
|
19
|
+
string message to be shown, or `null` if a generic default message should be
|
|
20
|
+
used.
|
|
21
|
+
|
|
22
|
+
By default, this map is empty, meaning no `jest` methods are banned.
|
|
23
|
+
|
|
24
|
+
For example:
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"jest/no-restricted-jest-methods": [
|
|
29
|
+
"error",
|
|
30
|
+
{
|
|
31
|
+
"advanceTimersByTime": null,
|
|
32
|
+
"spyOn": "Don't use spies"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Examples of **incorrect** code for this rule with the above configuration
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
jest.useFakeTimers();
|
|
42
|
+
it('calls the callback after 1 second via advanceTimersByTime', () => {
|
|
43
|
+
// ...
|
|
44
|
+
|
|
45
|
+
jest.advanceTimersByTime(1000);
|
|
46
|
+
|
|
47
|
+
// ...
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('plays video', () => {
|
|
51
|
+
const spy = jest.spyOn(video, 'play');
|
|
52
|
+
|
|
53
|
+
// ...
|
|
54
|
+
});
|
|
55
|
+
```
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
|
|
10
|
+
const messages = {
|
|
11
|
+
restrictedJestMethod: 'Use of `{{ restriction }}` is disallowed',
|
|
12
|
+
restrictedJestMethodWithMessage: '{{ message }}'
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
var _default = (0, _utils.createRule)({
|
|
16
|
+
name: __filename,
|
|
17
|
+
meta: {
|
|
18
|
+
docs: {
|
|
19
|
+
category: 'Best Practices',
|
|
20
|
+
description: 'Disallow specific `jest.` methods',
|
|
21
|
+
recommended: false
|
|
22
|
+
},
|
|
23
|
+
type: 'suggestion',
|
|
24
|
+
schema: [{
|
|
25
|
+
type: 'object',
|
|
26
|
+
additionalProperties: {
|
|
27
|
+
type: ['string', 'null']
|
|
28
|
+
}
|
|
29
|
+
}],
|
|
30
|
+
messages
|
|
31
|
+
},
|
|
32
|
+
defaultOptions: [{}],
|
|
33
|
+
|
|
34
|
+
create(context, [restrictedMethods]) {
|
|
35
|
+
return {
|
|
36
|
+
CallExpression(node) {
|
|
37
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
38
|
+
|
|
39
|
+
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'jest') {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const method = (0, _utils.getAccessorValue)(jestFnCall.members[0]);
|
|
44
|
+
|
|
45
|
+
if (method in restrictedMethods) {
|
|
46
|
+
const message = restrictedMethods[method];
|
|
47
|
+
context.report({
|
|
48
|
+
messageId: message ? 'restrictedJestMethodWithMessage' : 'restrictedJestMethod',
|
|
49
|
+
data: {
|
|
50
|
+
message,
|
|
51
|
+
restriction: method
|
|
52
|
+
},
|
|
53
|
+
loc: {
|
|
54
|
+
start: jestFnCall.members[0].loc.start,
|
|
55
|
+
end: jestFnCall.members[jestFnCall.members.length - 1].loc.end
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
exports.default = _default;
|
|
@@ -21,7 +21,11 @@ const isNullEqualityMatcher = expectFnCall => isNullLiteral((0, _utils2.getFirst
|
|
|
21
21
|
const isFirstArgumentIdentifier = (expectFnCall, name) => (0, _utils2.isIdentifier)((0, _utils2.getFirstMatcherArg)(expectFnCall), name);
|
|
22
22
|
|
|
23
23
|
const shouldUseToBe = expectFnCall => {
|
|
24
|
-
|
|
24
|
+
let firstArg = (0, _utils2.getFirstMatcherArg)(expectFnCall);
|
|
25
|
+
|
|
26
|
+
if (firstArg.type === _utils.AST_NODE_TYPES.UnaryExpression && firstArg.operator === '-') {
|
|
27
|
+
firstArg = firstArg.argument;
|
|
28
|
+
}
|
|
25
29
|
|
|
26
30
|
if (firstArg.type === _utils.AST_NODE_TYPES.Literal) {
|
|
27
31
|
// regex literals are classed as literals, but they're actually objects
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest",
|
|
3
|
-
"version": "27.
|
|
3
|
+
"version": "27.1.1",
|
|
4
4
|
"description": "ESLint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -110,6 +110,7 @@
|
|
|
110
110
|
"@semantic-release/git": "^10.0.0",
|
|
111
111
|
"@tsconfig/node14": "^1.0.3",
|
|
112
112
|
"@types/dedent": "^0.7.0",
|
|
113
|
+
"@types/eslint": "^8.4.6",
|
|
113
114
|
"@types/jest": "^29.0.0",
|
|
114
115
|
"@types/node": "^14.18.26",
|
|
115
116
|
"@types/prettier": "^2.0.0",
|