eslint-plugin-jest 25.4.0 → 25.5.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.5.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.4.0...v25.5.0) (2022-01-15)
2
+
3
+
4
+ ### Features
5
+
6
+ * **prefer-expect-assertions:** support requiring only if `expect` is used in a callback ([#1028](https://github.com/jest-community/eslint-plugin-jest/issues/1028)) ([8d5fd33](https://github.com/jest-community/eslint-plugin-jest/commit/8d5fd33eed633f0c0bbdcb9e86bd2d8d7de79c4b))
7
+
1
8
  # [25.4.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.4...v25.4.0) (2022-01-15)
2
9
 
3
10
 
@@ -157,3 +157,69 @@ describe('getNumbers', () => {
157
157
  });
158
158
  });
159
159
  ```
160
+
161
+ #### `onlyFunctionsWithExpectInCallback`
162
+
163
+ When `true`, this rule will only warn for tests that have `expect` calls within
164
+ a callback.
165
+
166
+ ```json
167
+ {
168
+ "rules": {
169
+ "jest/prefer-expect-assertions": [
170
+ "warn",
171
+ { "onlyFunctionsWithExpectInCallback": true }
172
+ ]
173
+ }
174
+ }
175
+ ```
176
+
177
+ Examples of **incorrect** code when `'onlyFunctionsWithExpectInCallback'` is
178
+ `true`:
179
+
180
+ ```js
181
+ describe('getNumbers', () => {
182
+ it('only returns numbers that are greater than zero', () => {
183
+ const numbers = getNumbers();
184
+
185
+ getNumbers().forEach(number => {
186
+ expect(number).toBeGreaterThan(0);
187
+ });
188
+ });
189
+ });
190
+
191
+ describe('/users', () => {
192
+ it.each([1, 2, 3])('returns ok', id => {
193
+ client.get(`/users/${id}`, response => {
194
+ expect(response.status).toBe(200);
195
+ });
196
+ });
197
+ });
198
+ ```
199
+
200
+ Examples of **correct** code when `'onlyFunctionsWithExpectInCallback'` is
201
+ `true`:
202
+
203
+ ```js
204
+ describe('getNumbers', () => {
205
+ it('only returns numbers that are greater than zero', () => {
206
+ expect.hasAssertions();
207
+
208
+ const numbers = getNumbers();
209
+
210
+ getNumbers().forEach(number => {
211
+ expect(number).toBeGreaterThan(0);
212
+ });
213
+ });
214
+ });
215
+
216
+ describe('/users', () => {
217
+ it.each([1, 2, 3])('returns ok', id => {
218
+ expect.assertions(3);
219
+
220
+ client.get(`/users/${id}`, response => {
221
+ expect(response.status).toBe(200);
222
+ });
223
+ });
224
+ });
225
+ ```
@@ -48,6 +48,9 @@ var _default = (0, _utils.createRule)({
48
48
  },
49
49
  onlyFunctionsWithExpectInLoop: {
50
50
  type: 'boolean'
51
+ },
52
+ onlyFunctionsWithExpectInCallback: {
53
+ type: 'boolean'
51
54
  }
52
55
  },
53
56
  additionalProperties: false
@@ -55,16 +58,19 @@ var _default = (0, _utils.createRule)({
55
58
  },
56
59
  defaultOptions: [{
57
60
  onlyFunctionsWithAsyncKeyword: false,
58
- onlyFunctionsWithExpectInLoop: false
61
+ onlyFunctionsWithExpectInLoop: false,
62
+ onlyFunctionsWithExpectInCallback: false
59
63
  }],
60
64
 
61
65
  create(context, [options]) {
66
+ let expressionDepth = 0;
67
+ let hasExpectInCallback = false;
62
68
  let hasExpectInLoop = false;
63
69
  let inTestCaseCall = false;
64
70
  let inForLoop = false;
65
71
 
66
72
  const shouldCheckFunction = testFunction => {
67
- if (!options.onlyFunctionsWithAsyncKeyword && !options.onlyFunctionsWithExpectInLoop) {
73
+ if (!options.onlyFunctionsWithAsyncKeyword && !options.onlyFunctionsWithExpectInLoop && !options.onlyFunctionsWithExpectInCallback) {
68
74
  return true;
69
75
  }
70
76
 
@@ -80,14 +86,28 @@ var _default = (0, _utils.createRule)({
80
86
  }
81
87
  }
82
88
 
89
+ if (options.onlyFunctionsWithExpectInCallback) {
90
+ if (hasExpectInCallback) {
91
+ return true;
92
+ }
93
+ }
94
+
83
95
  return false;
84
96
  };
85
97
 
98
+ const enterExpression = () => inTestCaseCall && expressionDepth++;
99
+
100
+ const exitExpression = () => inTestCaseCall && expressionDepth--;
101
+
86
102
  const enterForLoop = () => inForLoop = true;
87
103
 
88
104
  const exitForLoop = () => inForLoop = false;
89
105
 
90
106
  return {
107
+ FunctionExpression: enterExpression,
108
+ 'FunctionExpression:exit': exitExpression,
109
+ ArrowFunctionExpression: enterExpression,
110
+ 'ArrowFunctionExpression:exit': exitExpression,
91
111
  ForStatement: enterForLoop,
92
112
  'ForStatement:exit': exitForLoop,
93
113
  ForInStatement: enterForLoop,
@@ -101,8 +121,14 @@ var _default = (0, _utils.createRule)({
101
121
  return;
102
122
  }
103
123
 
104
- if ((0, _utils.isExpectCall)(node) && inTestCaseCall && inForLoop) {
105
- hasExpectInLoop = true;
124
+ if ((0, _utils.isExpectCall)(node) && inTestCaseCall) {
125
+ if (inForLoop) {
126
+ hasExpectInLoop = true;
127
+ }
128
+
129
+ if (expressionDepth > 1) {
130
+ hasExpectInCallback = true;
131
+ }
106
132
  }
107
133
  },
108
134
 
@@ -126,6 +152,7 @@ var _default = (0, _utils.createRule)({
126
152
  }
127
153
 
128
154
  hasExpectInLoop = false;
155
+ hasExpectInCallback = false;
129
156
  const testFuncBody = testFn.body.body;
130
157
 
131
158
  if (!isFirstLineExprStmt(testFuncBody)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "25.4.0",
3
+ "version": "25.5.0",
4
4
  "description": "Eslint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",