Skip to content

Commit e4c0daa

Browse files
authored
fix(no-wait-for-side-effects): detect await expressions (#1008)
Closes #742
1 parent 7077485 commit e4c0daa

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

lib/node-utils/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ export function getPropertyIdentifierNode(
378378
return getPropertyIdentifierNode(node.expression);
379379
}
380380

381+
if (ASTUtils.isAwaitExpression(node)) {
382+
return getPropertyIdentifierNode(node.argument);
383+
}
384+
381385
return null;
382386
}
383387

lib/rules/no-wait-for-multiple-assertions.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { TSESTree } from '@typescript-eslint/utils';
22

33
import { createTestingLibraryRule } from '../create-testing-library-rule';
4-
import {
5-
getPropertyIdentifierNode,
6-
isExpressionStatement,
7-
} from '../node-utils';
4+
import { getPropertyIdentifierNode } from '../node-utils';
85

96
export const RULE_NAME = 'no-wait-for-multiple-assertions';
107
export type MessageIds = 'noWaitForMultipleAssertion';
@@ -38,10 +35,6 @@ export default createTestingLibraryRule<Options, MessageIds>({
3835
body: Array<TSESTree.Node>
3936
): Array<TSESTree.ExpressionStatement> {
4037
return body.filter((node) => {
41-
if (!isExpressionStatement(node)) {
42-
return false;
43-
}
44-
4538
const expressionIdentifier = getPropertyIdentifierNode(node);
4639
if (!expressionIdentifier) {
4740
return false;

tests/lib/rules/no-wait-for-side-effects.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ ruleTester.run(RULE_NAME, rule, {
112112
// Side effects are allowed inside .then()
113113
userEvent.click(button);
114114
})
115+
`,
116+
},
117+
{
118+
code: `
119+
import { waitFor } from '${testingFramework}';
120+
import { notUserEvent } from 'somewhere-else';
121+
122+
waitFor(() => {
123+
await notUserEvent.click(button)
124+
})
115125
`,
116126
},
117127
]),
@@ -797,5 +807,21 @@ ruleTester.run(RULE_NAME, rule, {
797807
],
798808
} as const,
799809
]),
810+
811+
...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
812+
{
813+
code: `
814+
import { waitFor } from '${testingFramework}';
815+
import userEvent from '@testing-library/user-event'
816+
817+
it("some test", async () => {
818+
await waitFor(async () => {
819+
await fireEvent.click(screen.getByTestId("something"));
820+
});
821+
});
822+
`,
823+
errors: [{ line: 7, column: 13, messageId: 'noSideEffectsWaitFor' }],
824+
},
825+
]),
800826
],
801827
});

0 commit comments

Comments
 (0)