source: frontend/node_modules/eslint-plugin-jest/docs/rules/valid-expect-in-promise.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.9 KB
Line 
1# Ensure promises that have expectations in their chain are valid (`valid-expect-in-promise`)
2
3Ensure promises that include expectations are returned or awaited.
4
5## Rule details
6
7This rule flags any promises within the body of a test that include expectations
8that have either not been returned or awaited.
9
10The following patterns is considered warning:
11
12```js
13it('promises a person', () => {
14 api.getPersonByName('bob').then(person => {
15 expect(person).toHaveProperty('name', 'Bob');
16 });
17});
18
19it('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
29it('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
41The following pattern is not warning:
42
43```js
44it('promises a person', async () => {
45 await api.getPersonByName('bob').then(person => {
46 expect(person).toHaveProperty('name', 'Bob');
47 });
48});
49
50it('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
62it('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```
Note: See TracBrowser for help on using the repository browser.