Skip to content

Commit 9677748

Browse files
author
Charley
committed
fix: handled promises assigned to vars are valid
1 parent 35e2b40 commit 9677748

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/rules/await-async-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getFunctionName,
88
getInnermostReturningFunction,
99
getVariableReferences,
10+
isCallExpression,
1011
isObjectPattern,
1112
isPromiseHandled,
1213
isProperty,
@@ -110,6 +111,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
110111
const isAssigningKnownAsyncFunctionWrapper =
111112
ASTUtils.isIdentifier(node.id) &&
112113
node.init !== null &&
114+
!isCallExpression(node.init) &&
115+
!ASTUtils.isAwaitExpression(node.init) &&
113116
functionWrappersNames.includes(
114117
getDeepestIdentifierNode(node.init)?.name ?? ''
115118
);

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,20 @@ ruleTester.run(RULE_NAME, rule, {
250250
test('handled promise from function wrapping ${asyncUtil} util is valid', async () => {
251251
await waitForSomethingAsync()
252252
});
253+
`,
254+
})),
255+
...ASYNC_UTILS.map((asyncUtil) => ({
256+
code: `
257+
import { ${asyncUtil} } from '${testingFramework}';
258+
259+
function waitForSomethingAsync() {
260+
return ${asyncUtil}(() => somethingAsync())
261+
}
262+
263+
test('handled promise in variable declaration from function wrapping ${asyncUtil} util is valid', async () => {
264+
const result = await waitForSomethingAsync()
265+
expect(result).toBe('foo')
266+
});
253267
`,
254268
})),
255269
{
@@ -506,6 +520,32 @@ ruleTester.run(RULE_NAME, rule, {
506520
(asyncUtil) =>
507521
({
508522
code: `
523+
import { ${asyncUtil}, render } from '${testingFramework}';
524+
525+
function waitForSomethingAsync() {
526+
return ${asyncUtil}(() => somethingAsync())
527+
}
528+
529+
test('unhandled promise in variable declaration from function wrapping ${asyncUtil} util is invalid', async () => {
530+
render()
531+
const result = waitForSomethingAsync()
532+
expect(result).toBe('foo')
533+
});
534+
`,
535+
errors: [
536+
{
537+
messageId: 'asyncUtilWrapper',
538+
line: 10,
539+
column: 24,
540+
data: { name: 'waitForSomethingAsync' },
541+
},
542+
],
543+
}) as const
544+
),
545+
...ASYNC_UTILS.map(
546+
(asyncUtil) =>
547+
({
548+
code: `
509549
import { ${asyncUtil} } from 'some-other-library'; // rather than ${testingFramework}
510550
test(
511551
'aggressive reporting - util "${asyncUtil}" which is not related to testing library is invalid',

0 commit comments

Comments
 (0)