Skip to content

Commit

Permalink
feat(prefer-expect-assertions): support requiring only if expect is…
Browse files Browse the repository at this point in the history
… used in a callback (#1028)
  • Loading branch information
G-Rath committed Jan 15, 2022
1 parent 618a8dd commit 8d5fd33
Show file tree
Hide file tree
Showing 3 changed files with 545 additions and 3 deletions.
66 changes: 66 additions & 0 deletions docs/rules/prefer-expect-assertions.md
Expand Up @@ -157,3 +157,69 @@ describe('getNumbers', () => {
});
});
```

#### `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);
});
});
});
```

0 comments on commit 8d5fd33

Please sign in to comment.