eslint-plugin-jest 29.7.0 → 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,11 +394,12 @@ Manually fixable by
394
394
 
395
395
  ### Requires Type Checking
396
396
 
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 | | | | |
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 | | | | |
402
403
 
403
404
  <!-- end auto-generated rules list -->
404
405
 
@@ -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,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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "29.7.0",
3
+ "version": "29.8.0",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",