2022-11-10 10:43:16 +00:00
|
|
|
# Require promises that have expectations in their chain to be valid (`valid-expect-in-promise`)
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
💼 This rule is enabled in the ✅ `recommended`
|
|
|
|
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
<!-- end auto-generated rule header -->
|
|
|
|
|
|
|
|
Ensure promises that include expectations are returned or awaited.
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
## Rule details
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
This rule flags any promises within the body of a test that include expectations
|
|
|
|
that have either not been returned or awaited.
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
The following patterns is considered warning:
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
```js
|
2022-11-10 10:43:16 +00:00
|
|
|
it('promises a person', () => {
|
|
|
|
api.getPersonByName('bob').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Bob');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('promises a counted person', () => {
|
|
|
|
const promise = api.getPersonByName('bob').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Bob');
|
|
|
|
});
|
|
|
|
|
|
|
|
promise.then(() => {
|
|
|
|
expect(analytics.gottenPeopleCount).toBe(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('promises multiple people', () => {
|
|
|
|
const firstPromise = api.getPersonByName('bob').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Bob');
|
|
|
|
});
|
|
|
|
const secondPromise = api.getPersonByName('alice').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Alice');
|
2020-08-25 23:57:08 +00:00
|
|
|
});
|
2022-11-10 10:43:16 +00:00
|
|
|
|
|
|
|
return Promise.any([firstPromise, secondPromise]);
|
2020-08-25 23:57:08 +00:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
The following pattern is not warning:
|
|
|
|
|
|
|
|
```js
|
2022-11-10 10:43:16 +00:00
|
|
|
it('promises a person', async () => {
|
|
|
|
await api.getPersonByName('bob').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Bob');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('promises a counted person', () => {
|
|
|
|
let promise = api.getPersonByName('bob').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Bob');
|
|
|
|
});
|
|
|
|
|
|
|
|
promise = promise.then(() => {
|
|
|
|
expect(analytics.gottenPeopleCount).toBe(1);
|
2020-08-25 23:57:08 +00:00
|
|
|
});
|
2022-11-10 10:43:16 +00:00
|
|
|
|
|
|
|
return promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('promises multiple people', () => {
|
|
|
|
const firstPromise = api.getPersonByName('bob').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Bob');
|
|
|
|
});
|
|
|
|
const secondPromise = api.getPersonByName('alice').then(person => {
|
|
|
|
expect(person).toHaveProperty('name', 'Alice');
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.allSettled([firstPromise, secondPromise]);
|
2020-08-25 23:57:08 +00:00
|
|
|
});
|
|
|
|
```
|