Skip to content

Commit f2b13bb

Browse files
committed
feat(prefer-expect-assertions): support requiring only if expect is used in a callback
1 parent 6e5e94f commit f2b13bb

File tree

3 files changed

+545
-3
lines changed

3 files changed

+545
-3
lines changed

docs/rules/prefer-expect-assertions.md

+66
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,69 @@ describe('getNumbers', () => {
157157
});
158158
});
159159
```
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+
```

0 commit comments

Comments
 (0)