| 1 | # Ensure promises that have expectations in their chain are valid (`valid-expect-in-promise`)
|
|---|
| 2 |
|
|---|
| 3 | Ensure promises that include expectations are returned or awaited.
|
|---|
| 4 |
|
|---|
| 5 | ## Rule details
|
|---|
| 6 |
|
|---|
| 7 | This rule flags any promises within the body of a test that include expectations
|
|---|
| 8 | that have either not been returned or awaited.
|
|---|
| 9 |
|
|---|
| 10 | The following patterns is considered warning:
|
|---|
| 11 |
|
|---|
| 12 | ```js
|
|---|
| 13 | it('promises a person', () => {
|
|---|
| 14 | api.getPersonByName('bob').then(person => {
|
|---|
| 15 | expect(person).toHaveProperty('name', 'Bob');
|
|---|
| 16 | });
|
|---|
| 17 | });
|
|---|
| 18 |
|
|---|
| 19 | it('promises a counted person', () => {
|
|---|
| 20 | const promise = api.getPersonByName('bob').then(person => {
|
|---|
| 21 | expect(person).toHaveProperty('name', 'Bob');
|
|---|
| 22 | });
|
|---|
| 23 |
|
|---|
| 24 | promise.then(() => {
|
|---|
| 25 | expect(analytics.gottenPeopleCount).toBe(1);
|
|---|
| 26 | });
|
|---|
| 27 | });
|
|---|
| 28 |
|
|---|
| 29 | it('promises multiple people', () => {
|
|---|
| 30 | const firstPromise = api.getPersonByName('bob').then(person => {
|
|---|
| 31 | expect(person).toHaveProperty('name', 'Bob');
|
|---|
| 32 | });
|
|---|
| 33 | const secondPromise = api.getPersonByName('alice').then(person => {
|
|---|
| 34 | expect(person).toHaveProperty('name', 'Alice');
|
|---|
| 35 | });
|
|---|
| 36 |
|
|---|
| 37 | return Promise.any([firstPromise, secondPromise]);
|
|---|
| 38 | });
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | The following pattern is not warning:
|
|---|
| 42 |
|
|---|
| 43 | ```js
|
|---|
| 44 | it('promises a person', async () => {
|
|---|
| 45 | await api.getPersonByName('bob').then(person => {
|
|---|
| 46 | expect(person).toHaveProperty('name', 'Bob');
|
|---|
| 47 | });
|
|---|
| 48 | });
|
|---|
| 49 |
|
|---|
| 50 | it('promises a counted person', () => {
|
|---|
| 51 | let promise = api.getPersonByName('bob').then(person => {
|
|---|
| 52 | expect(person).toHaveProperty('name', 'Bob');
|
|---|
| 53 | });
|
|---|
| 54 |
|
|---|
| 55 | promise = promise.then(() => {
|
|---|
| 56 | expect(analytics.gottenPeopleCount).toBe(1);
|
|---|
| 57 | });
|
|---|
| 58 |
|
|---|
| 59 | return promise;
|
|---|
| 60 | });
|
|---|
| 61 |
|
|---|
| 62 | it('promises multiple people', () => {
|
|---|
| 63 | const firstPromise = api.getPersonByName('bob').then(person => {
|
|---|
| 64 | expect(person).toHaveProperty('name', 'Bob');
|
|---|
| 65 | });
|
|---|
| 66 | const secondPromise = api.getPersonByName('alice').then(person => {
|
|---|
| 67 | expect(person).toHaveProperty('name', 'Alice');
|
|---|
| 68 | });
|
|---|
| 69 |
|
|---|
| 70 | return Promise.allSettled([firstPromise, secondPromise]);
|
|---|
| 71 | });
|
|---|
| 72 | ```
|
|---|