|
1 |
| -# Enforce having return statement when testing with promises (`valid-expect-in-promise`) |
| 1 | +# Ensure promises that have expectations in their chain are valid (`valid-expect-in-promise`) |
2 | 2 |
|
3 |
| -Ensure to return promise when having assertions in `then` or `catch` block of |
4 |
| -promise |
| 3 | +Ensure promises that include expectations are returned or awaited. |
5 | 4 |
|
6 | 5 | ## Rule details
|
7 | 6 |
|
8 |
| -This rule looks for tests that have assertions in `then` and `catch` methods on |
9 |
| -promises that are not returned by the test. |
| 7 | +This rule flags any promises within the body of a test that include expectations |
| 8 | +that have either not been returned or awaited. |
10 | 9 |
|
11 |
| -### Default configuration |
12 |
| - |
13 |
| -The following pattern is considered warning: |
| 10 | +The following patterns is considered warning: |
14 | 11 |
|
15 | 12 | ```js
|
16 |
| -it('promise test', () => { |
17 |
| - somePromise.then(data => { |
18 |
| - expect(data).toEqual('foo'); |
| 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); |
19 | 26 | });
|
20 | 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 | +}); |
21 | 39 | ```
|
22 | 40 |
|
23 | 41 | The following pattern is not warning:
|
24 | 42 |
|
25 | 43 | ```js
|
26 |
| -it('promise test', () => { |
27 |
| - return somePromise.then(data => { |
28 |
| - expect(data).toEqual('foo'); |
| 44 | +it('promises a person', async () => { |
| 45 | + await api.getPersonByName('bob').then(person => { |
| 46 | + expect(person).toHaveProperty('name', 'Bob'); |
29 | 47 | });
|
30 | 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 | +}); |
31 | 72 | ```
|
0 commit comments