eslint-plugin-jest 29.6.1 → 29.8.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
CHANGED
|
@@ -394,10 +394,12 @@ Manually fixable by
|
|
|
394
394
|
|
|
395
395
|
### Requires Type Checking
|
|
396
396
|
|
|
397
|
-
| Name
|
|
398
|
-
|
|
|
399
|
-
| [no-
|
|
400
|
-
| [
|
|
397
|
+
| Name | Description | 💼 | ⚠️ | 🔧 | 💡 |
|
|
398
|
+
| :------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ | :-- | :-- | :-- | :-- |
|
|
399
|
+
| [no-error-equal](docs/rules/no-error-equal.md) | Disallow using equality matchers on error types | | | | |
|
|
400
|
+
| [no-unnecessary-assertion](docs/rules/no-unnecessary-assertion.md) | Disallow unnecessary assertions based on types | | | | |
|
|
401
|
+
| [unbound-method](docs/rules/unbound-method.md) | Enforce unbound methods are called with their expected scope | | | | |
|
|
402
|
+
| [valid-expect-with-promise](docs/rules/valid-expect-with-promise.md) | Require that `resolve` and `reject` modifiers are present (and only) for promise-like types | | | | |
|
|
401
403
|
|
|
402
404
|
<!-- end auto-generated rules list -->
|
|
403
405
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Disallow using equality matchers on error types (`no-error-equal`)
|
|
2
|
+
|
|
3
|
+
💭 This rule requires
|
|
4
|
+
[type information](https://typescript-eslint.io/linting/typed-linting).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
When comparing errors, `toEqual` and `toStrictEqual` will only compare the
|
|
9
|
+
`message` properties, meaning tests can pass even if the errors are of different
|
|
10
|
+
types.
|
|
11
|
+
|
|
12
|
+
Instead, it is better to use `toThrow` which does check the error type along
|
|
13
|
+
with its message.
|
|
14
|
+
|
|
15
|
+
## Rule details
|
|
16
|
+
|
|
17
|
+
This rule warns when `toEqual` and `toStrictEqual` is used with an `Error` type.
|
|
18
|
+
|
|
19
|
+
The following patterns are considered warnings:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
expect(new AggregateError([], expect.any(String))).toEqual(
|
|
23
|
+
new Error(expect.any(String)),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(new Error('hello world')).toStrictEqual('hello sunshine');
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The following patterns are not considered warnings:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
expect(() => throw new AggregateError([], expect.any(String))).toThrow(new Error(expect.any(String)));
|
|
33
|
+
|
|
34
|
+
expect(() => throw new Error('hello world')).toThrow('hello sunshine');
|
|
35
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Require that `resolve` and `reject` modifiers are present (and only) for promise-like types (`valid-expect-with-promise`)
|
|
2
|
+
|
|
3
|
+
💭 This rule requires
|
|
4
|
+
[type information](https://typescript-eslint.io/linting/typed-linting).
|
|
5
|
+
|
|
6
|
+
<!-- end auto-generated rule header -->
|
|
7
|
+
|
|
8
|
+
When working with promises, you must remember to use `resolves` and `rejects` to
|
|
9
|
+
assert on the value returned (or thrown) by the promise, rather than the promise
|
|
10
|
+
itself.
|
|
11
|
+
|
|
12
|
+
Inversely, while Jest does not prevent you from using `resolves` and `rejects`
|
|
13
|
+
on non-promise values, it is not necessary.
|
|
14
|
+
|
|
15
|
+
When TypeScript is in use, it is possible to determine when `resolves` and
|
|
16
|
+
`rejects` should and should not be needed.
|
|
17
|
+
|
|
18
|
+
## Rule details
|
|
19
|
+
|
|
20
|
+
This rule warns when:
|
|
21
|
+
|
|
22
|
+
- an `expect` is given a promise-like value but without `resolves` or `rejects`
|
|
23
|
+
- an `expect` is not given a promise-like value, but is used with `resolves` or
|
|
24
|
+
`rejects`
|
|
25
|
+
|
|
26
|
+
The following patterns are considered warnings:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
expect('hello world').resolves.toBe('hello sunshine');
|
|
30
|
+
|
|
31
|
+
expect(new Promise(r => r(0))).toThrow('oh noes!');
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The following patterns are not considered warnings:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
expect('hello world').toBe('hello sunshine');
|
|
38
|
+
|
|
39
|
+
expect(new Promise(r => r(0))).rejects.toThrow('oh noes!');
|
|
40
|
+
```
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
let SymbolFlags;
|
|
10
|
+
let TypeFlags;
|
|
11
|
+
function isSymbolFromDefaultLibrary(program, symbol) {
|
|
12
|
+
/* istanbul ignore next */
|
|
13
|
+
const declarations = symbol.getDeclarations() ?? [];
|
|
14
|
+
for (const declaration of declarations) {
|
|
15
|
+
const sourceFile = declaration.getSourceFile();
|
|
16
|
+
|
|
17
|
+
/* istanbul ignore else */
|
|
18
|
+
if (program.isSourceFileDefaultLibrary(sourceFile)) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/* istanbul ignore next */
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
function isBuiltinSymbolLike(program, type, symbolName) {
|
|
27
|
+
return isBuiltinSymbolLikeRecurser(program, type, subType => {
|
|
28
|
+
const symbol = subType.getSymbol();
|
|
29
|
+
if (!symbol) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const actualSymbolName = symbol.getName();
|
|
33
|
+
if (actualSymbolName === symbolName && isSymbolFromDefaultLibrary(program, symbol)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function isBuiltinSymbolLikeRecurser(program, type, predicate) {
|
|
40
|
+
if (type.isIntersection()) {
|
|
41
|
+
return type.types.some(t => isBuiltinSymbolLikeRecurser(program, t, predicate));
|
|
42
|
+
}
|
|
43
|
+
if (type.isUnion()) {
|
|
44
|
+
return type.types.every(t => isBuiltinSymbolLikeRecurser(program, t, predicate));
|
|
45
|
+
}
|
|
46
|
+
if (isTypeParameter(type)) {
|
|
47
|
+
const t = type.getConstraint();
|
|
48
|
+
if (t) {
|
|
49
|
+
return isBuiltinSymbolLikeRecurser(program, t, predicate);
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const predicateResult = predicate(type);
|
|
54
|
+
if (typeof predicateResult === 'boolean') {
|
|
55
|
+
return predicateResult;
|
|
56
|
+
}
|
|
57
|
+
const symbol = type.getSymbol();
|
|
58
|
+
|
|
59
|
+
/* istanbul ignore next */
|
|
60
|
+
if (symbol && symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
|
|
61
|
+
const checker = program.getTypeChecker();
|
|
62
|
+
for (const baseType of checker.getBaseTypes(type)) {
|
|
63
|
+
if (isBuiltinSymbolLikeRecurser(program, baseType, predicate)) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
function isTypeParameter(type) {
|
|
71
|
+
return (type.flags & TypeFlags.TypeParameter) !== 0;
|
|
72
|
+
}
|
|
73
|
+
var _default = exports.default = (0, _utils2.createRule)({
|
|
74
|
+
name: __filename,
|
|
75
|
+
meta: {
|
|
76
|
+
docs: {
|
|
77
|
+
description: 'Disallow using equality matchers on error types',
|
|
78
|
+
requiresTypeChecking: true
|
|
79
|
+
},
|
|
80
|
+
messages: {
|
|
81
|
+
equalError: 'Avoid using equality matchers to check errors'
|
|
82
|
+
},
|
|
83
|
+
type: 'suggestion',
|
|
84
|
+
schema: []
|
|
85
|
+
},
|
|
86
|
+
defaultOptions: [],
|
|
87
|
+
create(context) {
|
|
88
|
+
const services = _utils.ESLintUtils.getParserServices(context);
|
|
89
|
+
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
91
|
+
({
|
|
92
|
+
TypeFlags,
|
|
93
|
+
SymbolFlags
|
|
94
|
+
} = require('typescript'));
|
|
95
|
+
return {
|
|
96
|
+
CallExpression(node) {
|
|
97
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
98
|
+
if (jestFnCall?.type !== 'expect' || jestFnCall.head.node.parent.type !== _utils.AST_NODE_TYPES.CallExpression || !['toEqual', 'toStrictEqual'].includes((0, _utils2.getAccessorValue)(jestFnCall.matcher))) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const [argument] = jestFnCall.head.node.parent.arguments;
|
|
102
|
+
if (isBuiltinSymbolLike(services.program, services.getTypeAtLocation(argument), 'Error')) {
|
|
103
|
+
context.report({
|
|
104
|
+
messageId: 'equalError',
|
|
105
|
+
node
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
let SymbolFlags;
|
|
10
|
+
let TypeFlags;
|
|
11
|
+
function isSymbolFromDefaultLibrary(program, symbol) {
|
|
12
|
+
/* istanbul ignore next */
|
|
13
|
+
const declarations = symbol.getDeclarations() ?? [];
|
|
14
|
+
for (const declaration of declarations) {
|
|
15
|
+
const sourceFile = declaration.getSourceFile();
|
|
16
|
+
|
|
17
|
+
/* istanbul ignore else */
|
|
18
|
+
if (program.isSourceFileDefaultLibrary(sourceFile)) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/* istanbul ignore next */
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
function isBuiltinSymbolLike(program, type, symbolName) {
|
|
27
|
+
return isBuiltinSymbolLikeRecurser(program, type, subType => {
|
|
28
|
+
const symbol = subType.getSymbol();
|
|
29
|
+
if (!symbol) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const actualSymbolName = symbol.getName();
|
|
33
|
+
if (actualSymbolName === symbolName && isSymbolFromDefaultLibrary(program, symbol)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function isBuiltinSymbolLikeRecurser(program, type, predicate) {
|
|
40
|
+
if (type.isIntersection()) {
|
|
41
|
+
return type.types.some(t => isBuiltinSymbolLikeRecurser(program, t, predicate));
|
|
42
|
+
}
|
|
43
|
+
if (type.isUnion()) {
|
|
44
|
+
return type.types.every(t => isBuiltinSymbolLikeRecurser(program, t, predicate));
|
|
45
|
+
}
|
|
46
|
+
if (isTypeParameter(type)) {
|
|
47
|
+
const t = type.getConstraint();
|
|
48
|
+
if (t) {
|
|
49
|
+
return isBuiltinSymbolLikeRecurser(program, t, predicate);
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const predicateResult = predicate(type);
|
|
54
|
+
if (typeof predicateResult === 'boolean') {
|
|
55
|
+
return predicateResult;
|
|
56
|
+
}
|
|
57
|
+
const symbol = type.getSymbol();
|
|
58
|
+
|
|
59
|
+
/* istanbul ignore next */
|
|
60
|
+
if (symbol && symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
|
|
61
|
+
const checker = program.getTypeChecker();
|
|
62
|
+
for (const baseType of checker.getBaseTypes(type)) {
|
|
63
|
+
if (isBuiltinSymbolLikeRecurser(program, baseType, predicate)) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
function isTypeParameter(type) {
|
|
71
|
+
return (type.flags & TypeFlags.TypeParameter) !== 0;
|
|
72
|
+
}
|
|
73
|
+
var _default = exports.default = (0, _utils2.createRule)({
|
|
74
|
+
name: __filename,
|
|
75
|
+
meta: {
|
|
76
|
+
docs: {
|
|
77
|
+
description: 'Require that `resolve` and `reject` modifiers are present (and only) for promise-like types',
|
|
78
|
+
requiresTypeChecking: true
|
|
79
|
+
},
|
|
80
|
+
messages: {
|
|
81
|
+
poorlyExpectedPromise: 'Subject is a promise so resolve or reject should be used',
|
|
82
|
+
unneededRejectResolve: 'Subject is not a promise so {{ modifier }} is not needed'
|
|
83
|
+
},
|
|
84
|
+
type: 'suggestion',
|
|
85
|
+
schema: []
|
|
86
|
+
},
|
|
87
|
+
defaultOptions: [],
|
|
88
|
+
create(context) {
|
|
89
|
+
const services = _utils.ESLintUtils.getParserServices(context);
|
|
90
|
+
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
92
|
+
({
|
|
93
|
+
TypeFlags,
|
|
94
|
+
SymbolFlags
|
|
95
|
+
} = require('typescript'));
|
|
96
|
+
return {
|
|
97
|
+
CallExpression(node) {
|
|
98
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
99
|
+
if (jestFnCall?.type !== 'expect' || jestFnCall.head.node.parent.type !== _utils.AST_NODE_TYPES.CallExpression) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const [argument] = jestFnCall.head.node.parent.arguments;
|
|
103
|
+
const isPromiseLike = isBuiltinSymbolLike(services.program, services.getTypeAtLocation(argument), 'Promise');
|
|
104
|
+
const promiseModifier = jestFnCall.modifiers.find(nod => (0, _utils2.getAccessorValue)(nod) !== 'not');
|
|
105
|
+
if (isPromiseLike && !promiseModifier) {
|
|
106
|
+
context.report({
|
|
107
|
+
messageId: 'poorlyExpectedPromise',
|
|
108
|
+
node
|
|
109
|
+
});
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (!isPromiseLike && promiseModifier) {
|
|
113
|
+
context.report({
|
|
114
|
+
messageId: 'unneededRejectResolve',
|
|
115
|
+
data: {
|
|
116
|
+
modifier: (0, _utils2.getAccessorValue)(promiseModifier)
|
|
117
|
+
},
|
|
118
|
+
node: promiseModifier
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
});
|