Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prefer-expect-assertions): support requiring only if expect is used in a callback #1028

Merged
merged 1 commit into from Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});
});
```