Skip to content

Commit 9040b19

Browse files
feat: await-async-* handles finally
1 parent bef5e4b commit 9040b19

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

docs/rules/await-async-queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ problems in the tests. The promise will be considered as handled when:
2121

2222
- using the `await` operator
2323
- wrapped within `Promise.all` or `Promise.allSettled` methods
24-
- chaining the `then` or `catch` method
24+
- chaining the `then`, `catch`, `finally` method
2525
- chaining `resolves` or `rejects` from jest
2626
- chaining `toResolve()` or `toReject()` from [jest-extended](https://github.com/jest-community/jest-extended#promise)
2727
- chaining jasmine [async matchers](https://jasmine.github.io/api/edge/async-matchers.html)

docs/rules/await-async-utils.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ problems in the tests. The promise will be considered as handled when:
1919

2020
- using the `await` operator
2121
- wrapped within `Promise.all` or `Promise.allSettled` methods
22-
- chaining the `then` or `catch` method
22+
- chaining the `then`, `catch`, `finally` method
2323
- chaining `resolves` or `rejects` from jest
2424
- chaining `toResolve()` or `toReject()` from [jest-extended](https://github.com/jest-community/jest-extended#promise)
2525
- chaining jasmine [async matchers](https://jasmine.github.io/api/edge/async-matchers.html)

lib/node-utils/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,19 @@ export function hasPromiseHandlerProperty(node: TSESTree.Node): boolean {
155155
return (
156156
isMemberExpression(node) &&
157157
ASTUtils.isIdentifier(node.property) &&
158-
['then', 'catch'].includes(node.property.name)
158+
['then', 'catch', 'finally'].includes(node.property.name)
159159
);
160160
}
161161

162162
export function hasChainedPromiseHandler(node: TSESTree.Node): boolean {
163163
const parent = node.parent;
164164

165-
// wait(...).then(...) or wait(...).catch(...)
165+
// wait(...).then(...) or wait(...).catch(...) or wait(...).finally(...)
166166
if (isCallExpression(parent) && parent.parent) {
167167
return hasPromiseHandlerProperty(parent.parent);
168168
}
169169

170-
// promise.then(...) or promise.catch(...)
170+
// promise.then(...) or promise.catch(...) or promise(...).finally(...)
171171
return !!parent && hasPromiseHandlerProperty(parent);
172172
}
173173

@@ -222,7 +222,7 @@ export function isPromisesArrayResolved(node: TSESTree.Node): boolean {
222222
* - it belongs to the `await` expression
223223
* - it belongs to the `Promise.all` method
224224
* - it belongs to the `Promise.allSettled` method
225-
* - it's chained with the `then` or `catch` method
225+
* - it's chained with the `then` `catch` `finally` method
226226
* - it's returned from a function
227227
* - has `resolves` or `rejects` jest methods
228228
* - has `toResolve` or `toReject` jest-extended matchers

tests/lib/rules/await-async-events.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ ruleTester.run(RULE_NAME, rule, {
109109
`,
110110
options: [{ eventModule: 'fireEvent' }] as const,
111111
})),
112+
...FIRE_EVENT_ASYNC_FUNCTIONS.map((eventMethod) => ({
113+
code: `
114+
import { fireEvent } from '${testingFramework}'
115+
test('chain finally method to promise from event method is valid', async (done) => {
116+
fireEvent.${eventMethod}(getByLabelText('username'))
117+
.finally(() => { done() })
118+
})
119+
`,
120+
options: [{ eventModule: 'fireEvent' }] as const,
121+
})),
112122
{
113123
code: `
114124
import { fireEvent } from '${testingFramework}'
@@ -350,6 +360,16 @@ ruleTester.run(RULE_NAME, rule, {
350360
...USER_EVENT_ASYNC_FUNCTIONS.map((eventMethod) => ({
351361
code: `
352362
import userEvent from '${testingFramework}'
363+
test('chain finally method to promise from event method is valid', async (done) => {
364+
userEvent.${eventMethod}(getByLabelText('username'))
365+
.finally(() => { done() })
366+
})
367+
`,
368+
options: [{ eventModule: 'userEvent' }] as const,
369+
})),
370+
...USER_EVENT_ASYNC_FUNCTIONS.map((eventMethod) => ({
371+
code: `
372+
import userEvent from '${testingFramework}'
353373
test('chain then method to several promises from event methods is valid', async (done) => {
354374
userEvent.${eventMethod}(getByLabelText('username')).then(() => {
355375
userEvent.${eventMethod}(getByLabelText('username')).then(() => { done() })

tests/lib/rules/await-async-queries.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ ruleTester.run(RULE_NAME, rule, {
169169
`
170170
),
171171

172+
// async queries are valid with finally
173+
...createTestCase(
174+
(query) => `
175+
const promise = ${query}('foo')
176+
promise.finally((done) => done())
177+
`
178+
),
179+
172180
// async queries are valid when wrapped within Promise.all + await expression
173181
...createTestCase(
174182
(query) => `

tests/lib/rules/await-async-utils.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ ruleTester.run(RULE_NAME, rule, {
5757
doSomethingElse();
5858
${asyncUtil}(() => getByLabelText('email')).catch((error) => { console.log('done') });
5959
});
60+
`,
61+
})),
62+
...ASYNC_UTILS.map((asyncUtil) => ({
63+
code: `
64+
import { ${asyncUtil} } from '${testingFramework}';
65+
test('${asyncUtil} util directly chained with finally is valid', () => {
66+
doSomethingElse();
67+
${asyncUtil}(() => getByLabelText('email')).finally(() => { console.log('done') });
68+
});
6069
`,
6170
})),
6271
...ASYNC_UTILS.map((asyncUtil) => ({

0 commit comments

Comments
 (0)