-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from playwright-community/valid-expect-in-pro…
…mise Valid expect in promise
- Loading branch information
Showing
7 changed files
with
2,230 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Require promises that have expectations in their chain to be valid (`valid-expect-in-promise`) | ||
|
||
Ensure promises that include expectations are returned or awaited. | ||
|
||
## Rule details | ||
|
||
This rule flags any promises within the body of a test that include expectations | ||
that have either not been returned or awaited. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
test('promises a person', () => { | ||
api.getPersonByName('bob').then((person) => { | ||
expect(person).toHaveProperty('name', 'Bob'); | ||
}); | ||
}); | ||
|
||
test('promises a counted person', () => { | ||
const promise = api.getPersonByName('bob').then((person) => { | ||
expect(person).toHaveProperty('name', 'Bob'); | ||
}); | ||
|
||
promise.then(() => { | ||
expect(analytics.gottenPeopleCount).toBe(1); | ||
}); | ||
}); | ||
|
||
test('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.any([firstPromise, secondPromise]); | ||
}); | ||
``` | ||
|
||
The following pattern is not a warning: | ||
|
||
```js | ||
test('promises a person', async () => { | ||
await api.getPersonByName('bob').then((person) => { | ||
expect(person).toHaveProperty('name', 'Bob'); | ||
}); | ||
}); | ||
|
||
test('promises a counted person', () => { | ||
let promise = api.getPersonByName('bob').then((person) => { | ||
expect(person).toHaveProperty('name', 'Bob'); | ||
}); | ||
|
||
promise = promise.then(() => { | ||
expect(analytics.gottenPeopleCount).toBe(1); | ||
}); | ||
|
||
return promise; | ||
}); | ||
|
||
test('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]); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.