Skip to content

Commit 03131e0

Browse files
committed
docs(await-async-query): update rule description and examples
1 parent 11e1b1f commit 03131e0

File tree

4 files changed

+53
-42
lines changed

4 files changed

+53
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ To enable this configuration use the `extends` property in your
127127

128128
| Rule | Description | Configurations | Fixable |
129129
| -------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | ------------------ |
130-
| [await-async-query](docs/rules/await-async-query.md) | Enforce async queries to have proper `await` | ![dom-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
130+
| [await-async-query](docs/rules/await-async-query.md) | Enforce promise from async queries to be handled | ![dom-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
131131
| [await-async-utils](docs/rules/await-async-utils.md) | Enforce async utils to be awaited properly | ![dom-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
132132
| [await-fire-event](docs/rules/await-fire-event.md) | Enforce async fire event methods to be awaited | ![vue-badge][] | |
133133
| [consistent-data-testid](docs/rules/consistent-data-testid.md) | Ensure `data-testid` values match a provided regex. | | |

docs/rules/await-async-query.md

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Enforce async queries to have proper `await` (await-async-query)
1+
# Enforce promise from async queries to be handled (await-async-query)
22

33
Ensure that promises returned by async queries are handled properly.
44

@@ -11,65 +11,76 @@ found. Those queries variants are:
1111
- `findBy*`
1212
- `findAllBy*`
1313

14-
This rule aims to prevent users from forgetting to await the returned
14+
This rule aims to prevent users from forgetting to handle the returned
1515
promise from those async queries to be fulfilled, which could lead to
16-
errors in the tests. The promises can be handled by using either `await`
17-
operator or `then` method.
16+
errors in the tests. The promise will be considered as handled when:
17+
18+
- using `await` operator
19+
- chaining `then` method
20+
- chaining `resolves` or `rejects` from jest
21+
- is returned from a function (in this case, that particular function will be analyzed by this rule too)
1822

1923
Examples of **incorrect** code for this rule:
2024

2125
```js
22-
const foo = () => {
23-
// ...
24-
const rows = findAllByRole('row');
25-
// ...
26-
};
27-
28-
const bar = () => {
29-
// ...
30-
findByText('submit');
31-
// ...
32-
};
33-
34-
const baz = () => {
35-
// ...
36-
screen.findAllByPlaceholderText('name');
37-
// ...
38-
};
26+
// async query without handling promise
27+
const rows = findAllByRole('row');
28+
29+
findByIcon('search');
30+
31+
screen.findAllByPlaceholderText('name');
32+
```
33+
34+
```js
35+
// promise from async query returned within wrapper function without being handled
36+
const findMyButton = () => findByText('my button');
37+
38+
const someButton = findMyButton(); // promise unhandled here
3939
```
4040

4141
Examples of **correct** code for this rule:
4242

4343
```js
4444
// `await` operator is correct
45-
const foo = async () => {
46-
// ...
47-
const rows = await findAllByRole('row');
48-
// ...
49-
};
45+
const rows = await findAllByRole('row');
46+
47+
await screen.findAllByPlaceholderText('name');
48+
49+
const promise = findByIcon('search');
50+
const element = await promise;
51+
```
5052

53+
```js
5154
// `then` method is correct
52-
const bar = () => {
53-
// ...
54-
findByText('submit').then(() => {
55-
// ...
56-
});
57-
};
58-
59-
const baz = () => {
60-
// ...
61-
await screen.findAllByPlaceholderText('name');
62-
// ...
63-
};
55+
findByText('submit').then(() => {});
6456

57+
const promise = findByRole('button');
58+
promise.then(() => {});
59+
```
60+
61+
```js
6562
// return the promise within a function is correct too!
6663
const findMyButton = () => findByText('my button');
64+
```
65+
66+
```js
67+
// promise from async query returned within wrapper function being handled
68+
const findMyButton = () => findByText('my button');
6769

70+
const someButton = await findMyButton();
71+
```
72+
73+
```js
6874
// using a resolves/rejects matcher is also correct
6975
expect(findByTestId('alert')).resolves.toBe('Success');
7076
expect(findByTestId('alert')).rejects.toBe('Error');
7177
```
7278

79+
```js
80+
// sync queries don't need to handle any promise
81+
const element = getByRole('role');
82+
```
83+
7384
## Further Reading
7485

7586
- [Async queries variants](https://testing-library.com/docs/dom-testing-library/api-queries#findby)

lib/node-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ export function hasChainedThen(node: TSESTree.Node): boolean {
216216
* Determines whether an Identifier related to a promise is considered as handled.
217217
*
218218
* It will be considered as handled if:
219-
* - it belongs to await expression
219+
* - it belongs to `await` expression
220+
* - it's chained with `then` method
220221
* - it's returned from a function
221-
* - it's resolved with `then` method
222222
* - has `resolves` or `rejects`
223223
*/
224224
export function isPromiseHandled(nodeIdentifier: TSESTree.Identifier): boolean {

lib/rules/await-async-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
1919
meta: {
2020
type: 'problem',
2121
docs: {
22-
description: 'Enforce async queries to have proper `await`',
22+
description: 'Enforce promise from async queries to be handled',
2323
category: 'Best Practices',
2424
recommended: 'warn',
2525
},

0 commit comments

Comments
 (0)