Skip to content

Commit c3504a7

Browse files
authored
fix(no-wait-for-side-effects): false negatives in variables declarations (#677)
fix: false negatives in variables declarations Closes #368
1 parent e2a08f4 commit c3504a7

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

+41
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,40 @@ export default createTestingLibraryRule<Options, MessageIds>({
120120
return node.expressions.some(isRenderInAssignmentExpression);
121121
}
122122

123+
/**
124+
* Checks if there are side effects in variable declarations.
125+
*
126+
* For example, these variable declarations have side effects:
127+
* const a = userEvent.doubleClick(button);
128+
* const b = fireEvent.click(button);
129+
* const wrapper = render(<Component />);
130+
*
131+
* @param node
132+
* @returns {Boolean} Boolean indicating if variable declarataion has side effects
133+
*/
134+
function isSideEffectInVariableDeclaration(
135+
node: TSESTree.VariableDeclaration
136+
): boolean {
137+
return node.declarations.some((declaration) => {
138+
if (isCallExpression(declaration.init)) {
139+
const test = getPropertyIdentifierNode(declaration.init);
140+
141+
if (!test) {
142+
return false;
143+
}
144+
145+
return (
146+
helpers.isFireEventUtil(test) ||
147+
helpers.isUserEventUtil(test) ||
148+
helpers.isRenderUtil(test)
149+
);
150+
}
151+
return false;
152+
});
153+
154+
return false;
155+
}
156+
123157
function getSideEffectNodes(
124158
body: TSESTree.Node[]
125159
): TSESTree.ExpressionStatement[] {
@@ -135,6 +169,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
135169
return true;
136170
}
137171

172+
if (
173+
isVariableDeclaration(node) &&
174+
isSideEffectInVariableDeclaration(node)
175+
) {
176+
return true;
177+
}
178+
138179
const expressionIdentifier = getPropertyIdentifierNode(node);
139180

140181
if (!expressionIdentifier) {

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

+20
Original file line numberDiff line numberDiff line change
@@ -795,5 +795,25 @@ ruleTester.run(RULE_NAME, rule, {
795795
{ line: 12, column: 13, messageId: 'noSideEffectsWaitFor' },
796796
],
797797
},
798+
// side effects (userEvent, fireEvent or render) in variable declarations
799+
...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
800+
{
801+
// Issue #368, https://github.com/testing-library/eslint-plugin-testing-library/issues/368
802+
code: `
803+
import { waitFor } from '${testingFramework}';
804+
import userEvent from '@testing-library/user-event'
805+
await waitFor(() => {
806+
const a = userEvent.click(button);
807+
const b = fireEvent.click(button);
808+
const wrapper = render(<App />);
809+
})
810+
`,
811+
errors: [
812+
{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' },
813+
{ line: 6, column: 11, messageId: 'noSideEffectsWaitFor' },
814+
{ line: 7, column: 11, messageId: 'noSideEffectsWaitFor' },
815+
],
816+
} as const,
817+
]),
798818
],
799819
});

0 commit comments

Comments
 (0)