Skip to content

Commit

Permalink
Merge pull request #255 from playwright-community/valid-expect-in-pro…
Browse files Browse the repository at this point in the history
…mise

Valid expect in promise
  • Loading branch information
mskelton authored Feb 27, 2024
2 parents d403f56 + 83b5f3b commit 560b732
Show file tree
Hide file tree
Showing 7 changed files with 2,230 additions and 53 deletions.
93 changes: 47 additions & 46 deletions README.md

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions docs/rules/valid-expect-in-promise.md
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]);
});
```
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import requireSoftAssertions from './rules/require-soft-assertions';
import requireTopLevelDescribe from './rules/require-top-level-describe';
import validDescribeCallback from './rules/valid-describe-callback';
import validExpect from './rules/valid-expect';
import validExpectInPromise from './rules/valid-expect-in-promise';
import validTitle from './rules/valid-title';

const index = {
Expand Down Expand Up @@ -90,6 +91,7 @@ const index = {
'require-top-level-describe': requireTopLevelDescribe,
'valid-describe-callback': validDescribeCallback,
'valid-expect': validExpect,
'valid-expect-in-promise': validExpectInPromise,
'valid-title': validTitle,
},
};
Expand Down Expand Up @@ -119,6 +121,7 @@ const sharedConfig = {
'playwright/prefer-web-first-assertions': 'error',
'playwright/valid-describe-callback': 'error',
'playwright/valid-expect': 'error',
'playwright/valid-expect-in-promise': 'error',
'playwright/valid-title': 'error',
},
};
Expand Down
Loading

0 comments on commit 560b732

Please sign in to comment.