eslint-plugin-jest 25.3.4 → 25.4.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/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [25.4.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.4...v25.4.0) (2022-01-15)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **prefer-expect-assertions:** support requiring only if `expect` is used in a loop ([#1013](https://github.com/jest-community/eslint-plugin-jest/issues/1013)) ([e6f4f8a](https://github.com/jest-community/eslint-plugin-jest/commit/e6f4f8aaf7664bcf9d9d5549c3c43b1b09f49461))
|
|
7
|
+
|
|
1
8
|
## [25.3.4](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.3...v25.3.4) (2022-01-01)
|
|
2
9
|
|
|
3
10
|
|
|
@@ -58,6 +58,16 @@ test('my test', () => {
|
|
|
58
58
|
|
|
59
59
|
## Options
|
|
60
60
|
|
|
61
|
+
This rule can be configured to only check tests that match certain patterns that
|
|
62
|
+
typically look like `expect` calls might be missed, such as in promises or
|
|
63
|
+
loops.
|
|
64
|
+
|
|
65
|
+
By default, none of these options are enabled meaning the rule checks _every_
|
|
66
|
+
test for a call to either `expect.hasAssertions` or `expect.assertions`. If any
|
|
67
|
+
of the options are enabled the rule checks any test that matches _at least one_
|
|
68
|
+
of the patterns represented by the enabled options (think "OR" rather than
|
|
69
|
+
"AND").
|
|
70
|
+
|
|
61
71
|
#### `onlyFunctionsWithAsyncKeyword`
|
|
62
72
|
|
|
63
73
|
When `true`, this rule will only warn for tests that use the `async` keyword.
|
|
@@ -97,3 +107,53 @@ test('my test', async () => {
|
|
|
97
107
|
expect(result).toBe('foo');
|
|
98
108
|
});
|
|
99
109
|
```
|
|
110
|
+
|
|
111
|
+
#### `onlyFunctionsWithExpectInLoop`
|
|
112
|
+
|
|
113
|
+
When `true`, this rule will only warn for tests that have `expect` calls within
|
|
114
|
+
a native loop.
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"rules": {
|
|
119
|
+
"jest/prefer-expect-assertions": [
|
|
120
|
+
"warn",
|
|
121
|
+
{ "onlyFunctionsWithAsyncKeyword": true }
|
|
122
|
+
]
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Examples of **incorrect** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
describe('getNumbers', () => {
|
|
131
|
+
it('only returns numbers that are greater than zero', () => {
|
|
132
|
+
const numbers = getNumbers();
|
|
133
|
+
|
|
134
|
+
for (const number in numbers) {
|
|
135
|
+
expect(number).toBeGreaterThan(0);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Examples of **correct** code when `'onlyFunctionsWithExpectInLoop'` is `true`:
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
describe('getNumbers', () => {
|
|
145
|
+
it('only returns numbers that are greater than zero', () => {
|
|
146
|
+
expect.hasAssertions();
|
|
147
|
+
|
|
148
|
+
const numbers = getNumbers();
|
|
149
|
+
|
|
150
|
+
for (const number in numbers) {
|
|
151
|
+
expect(number).toBeGreaterThan(0);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('returns more than one number', () => {
|
|
156
|
+
expect(getNumbers().length).toBeGreaterThan(1);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
```
|
|
@@ -45,18 +45,68 @@ var _default = (0, _utils.createRule)({
|
|
|
45
45
|
properties: {
|
|
46
46
|
onlyFunctionsWithAsyncKeyword: {
|
|
47
47
|
type: 'boolean'
|
|
48
|
+
},
|
|
49
|
+
onlyFunctionsWithExpectInLoop: {
|
|
50
|
+
type: 'boolean'
|
|
48
51
|
}
|
|
49
52
|
},
|
|
50
53
|
additionalProperties: false
|
|
51
54
|
}]
|
|
52
55
|
},
|
|
53
56
|
defaultOptions: [{
|
|
54
|
-
onlyFunctionsWithAsyncKeyword: false
|
|
57
|
+
onlyFunctionsWithAsyncKeyword: false,
|
|
58
|
+
onlyFunctionsWithExpectInLoop: false
|
|
55
59
|
}],
|
|
56
60
|
|
|
57
61
|
create(context, [options]) {
|
|
62
|
+
let hasExpectInLoop = false;
|
|
63
|
+
let inTestCaseCall = false;
|
|
64
|
+
let inForLoop = false;
|
|
65
|
+
|
|
66
|
+
const shouldCheckFunction = testFunction => {
|
|
67
|
+
if (!options.onlyFunctionsWithAsyncKeyword && !options.onlyFunctionsWithExpectInLoop) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (options.onlyFunctionsWithAsyncKeyword) {
|
|
72
|
+
if (testFunction.async) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (options.onlyFunctionsWithExpectInLoop) {
|
|
78
|
+
if (hasExpectInLoop) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return false;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const enterForLoop = () => inForLoop = true;
|
|
87
|
+
|
|
88
|
+
const exitForLoop = () => inForLoop = false;
|
|
89
|
+
|
|
58
90
|
return {
|
|
91
|
+
ForStatement: enterForLoop,
|
|
92
|
+
'ForStatement:exit': exitForLoop,
|
|
93
|
+
ForInStatement: enterForLoop,
|
|
94
|
+
'ForInStatement:exit': exitForLoop,
|
|
95
|
+
ForOfStatement: enterForLoop,
|
|
96
|
+
'ForOfStatement:exit': exitForLoop,
|
|
97
|
+
|
|
59
98
|
CallExpression(node) {
|
|
99
|
+
if ((0, _utils.isTestCaseCall)(node)) {
|
|
100
|
+
inTestCaseCall = true;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if ((0, _utils.isExpectCall)(node) && inTestCaseCall && inForLoop) {
|
|
105
|
+
hasExpectInLoop = true;
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
'CallExpression:exit'(node) {
|
|
60
110
|
if (!(0, _utils.isTestCaseCall)(node)) {
|
|
61
111
|
return;
|
|
62
112
|
}
|
|
@@ -67,10 +117,15 @@ var _default = (0, _utils.createRule)({
|
|
|
67
117
|
|
|
68
118
|
const [, testFn] = node.arguments;
|
|
69
119
|
|
|
70
|
-
if (!(0, _utils.isFunction)(testFn) || testFn.body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement
|
|
120
|
+
if (!(0, _utils.isFunction)(testFn) || testFn.body.type !== _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!shouldCheckFunction(testFn)) {
|
|
71
125
|
return;
|
|
72
126
|
}
|
|
73
127
|
|
|
128
|
+
hasExpectInLoop = false;
|
|
74
129
|
const testFuncBody = testFn.body.body;
|
|
75
130
|
|
|
76
131
|
if (!isFirstLineExprStmt(testFuncBody)) {
|