Skip to content

Commit

Permalink
feat: Add valid-expect-in-promise rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Feb 27, 2024
1 parent 841fd1d commit cbf356d
Show file tree
Hide file tree
Showing 7 changed files with 2,229 additions and 52 deletions.
91 changes: 46 additions & 45 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 @@ -41,6 +41,7 @@ import requireHook from './rules/require-hook';
import requireSoftAssertions from './rules/require-soft-assertions';
import requireTopLevelDescribe from './rules/require-top-level-describe';
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 @@ -88,6 +89,7 @@ const index = {
'require-soft-assertions': requireSoftAssertions,
'require-top-level-describe': requireTopLevelDescribe,
'valid-expect': validExpect,
'valid-expect-in-promise': validExpectInPromise,
'valid-title': validTitle,
},
};
Expand Down Expand Up @@ -116,6 +118,7 @@ const sharedConfig = {
'playwright/no-wait-for-timeout': 'warn',
'playwright/prefer-web-first-assertions': 'error',
'playwright/valid-expect': 'error',
'playwright/valid-expect-in-promise': 'error',
'playwright/valid-title': 'error',
},
};
Expand Down
Loading

0 comments on commit cbf356d

Please sign in to comment.