eslint-plugin-jest
Advanced tools
Comparing version 25.4.0 to 25.5.0
@@ -0,1 +1,8 @@ | ||
# [25.5.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.4.0...v25.5.0) (2022-01-15) | ||
### Features | ||
* **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)) | ||
# [25.4.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.4...v25.4.0) (2022-01-15) | ||
@@ -2,0 +9,0 @@ |
@@ -160,1 +160,67 @@ # Suggest using `expect.assertions()` OR `expect.hasAssertions()` (`prefer-expect-assertions`) | ||
``` | ||
#### `onlyFunctionsWithExpectInCallback` | ||
When `true`, this rule will only warn for tests that have `expect` calls within | ||
a callback. | ||
```json | ||
{ | ||
"rules": { | ||
"jest/prefer-expect-assertions": [ | ||
"warn", | ||
{ "onlyFunctionsWithExpectInCallback": true } | ||
] | ||
} | ||
} | ||
``` | ||
Examples of **incorrect** code when `'onlyFunctionsWithExpectInCallback'` is | ||
`true`: | ||
```js | ||
describe('getNumbers', () => { | ||
it('only returns numbers that are greater than zero', () => { | ||
const numbers = getNumbers(); | ||
getNumbers().forEach(number => { | ||
expect(number).toBeGreaterThan(0); | ||
}); | ||
}); | ||
}); | ||
describe('/users', () => { | ||
it.each([1, 2, 3])('returns ok', id => { | ||
client.get(`/users/${id}`, response => { | ||
expect(response.status).toBe(200); | ||
}); | ||
}); | ||
}); | ||
``` | ||
Examples of **correct** code when `'onlyFunctionsWithExpectInCallback'` is | ||
`true`: | ||
```js | ||
describe('getNumbers', () => { | ||
it('only returns numbers that are greater than zero', () => { | ||
expect.hasAssertions(); | ||
const numbers = getNumbers(); | ||
getNumbers().forEach(number => { | ||
expect(number).toBeGreaterThan(0); | ||
}); | ||
}); | ||
}); | ||
describe('/users', () => { | ||
it.each([1, 2, 3])('returns ok', id => { | ||
expect.assertions(3); | ||
client.get(`/users/${id}`, response => { | ||
expect(response.status).toBe(200); | ||
}); | ||
}); | ||
}); | ||
``` |
@@ -51,2 +51,5 @@ "use strict"; | ||
type: 'boolean' | ||
}, | ||
onlyFunctionsWithExpectInCallback: { | ||
type: 'boolean' | ||
} | ||
@@ -59,6 +62,9 @@ }, | ||
onlyFunctionsWithAsyncKeyword: false, | ||
onlyFunctionsWithExpectInLoop: false | ||
onlyFunctionsWithExpectInLoop: false, | ||
onlyFunctionsWithExpectInCallback: false | ||
}], | ||
create(context, [options]) { | ||
let expressionDepth = 0; | ||
let hasExpectInCallback = false; | ||
let hasExpectInLoop = false; | ||
@@ -69,3 +75,3 @@ let inTestCaseCall = false; | ||
const shouldCheckFunction = testFunction => { | ||
if (!options.onlyFunctionsWithAsyncKeyword && !options.onlyFunctionsWithExpectInLoop) { | ||
if (!options.onlyFunctionsWithAsyncKeyword && !options.onlyFunctionsWithExpectInLoop && !options.onlyFunctionsWithExpectInCallback) { | ||
return true; | ||
@@ -86,5 +92,15 @@ } | ||
if (options.onlyFunctionsWithExpectInCallback) { | ||
if (hasExpectInCallback) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
const enterExpression = () => inTestCaseCall && expressionDepth++; | ||
const exitExpression = () => inTestCaseCall && expressionDepth--; | ||
const enterForLoop = () => inForLoop = true; | ||
@@ -95,2 +111,6 @@ | ||
return { | ||
FunctionExpression: enterExpression, | ||
'FunctionExpression:exit': exitExpression, | ||
ArrowFunctionExpression: enterExpression, | ||
'ArrowFunctionExpression:exit': exitExpression, | ||
ForStatement: enterForLoop, | ||
@@ -109,4 +129,10 @@ 'ForStatement:exit': exitForLoop, | ||
if ((0, _utils.isExpectCall)(node) && inTestCaseCall && inForLoop) { | ||
hasExpectInLoop = true; | ||
if ((0, _utils.isExpectCall)(node) && inTestCaseCall) { | ||
if (inForLoop) { | ||
hasExpectInLoop = true; | ||
} | ||
if (expressionDepth > 1) { | ||
hasExpectInCallback = true; | ||
} | ||
} | ||
@@ -135,2 +161,3 @@ }, | ||
hasExpectInLoop = false; | ||
hasExpectInCallback = false; | ||
const testFuncBody = testFn.body.body; | ||
@@ -137,0 +164,0 @@ |
{ | ||
"name": "eslint-plugin-jest", | ||
"version": "25.4.0", | ||
"version": "25.5.0", | ||
"description": "Eslint rules for Jest", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
303043
4398