Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-wait-for-side-effects): false negatives in variables declarations #677

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/rules/no-wait-for-side-effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@ export default createTestingLibraryRule<Options, MessageIds>({
return node.expressions.some(isRenderInAssignmentExpression);
}

/**
* Checks if there are side effects in variable declarations.
*
* For example, these variable declarations have side effects:
* const a = userEvent.doubleClick(button);
* const b = fireEvent.click(button);
* const wrapper = render(<Component />);
*
* @param node
* @returns {Boolean} Boolean indicating if variable declarataion has side effects
*/
function isSideEffectInVariableDeclaration(
node: TSESTree.VariableDeclaration
): boolean {
return node.declarations.some((declaration) => {
if (isCallExpression(declaration.init)) {
const test = getPropertyIdentifierNode(declaration.init);

if (!test) {
return false;
}

return (
helpers.isFireEventUtil(test) ||
helpers.isUserEventUtil(test) ||
helpers.isRenderUtil(test)
);
}
return false;
});

return false;
}

function getSideEffectNodes(
body: TSESTree.Node[]
): TSESTree.ExpressionStatement[] {
Expand All @@ -135,6 +169,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
return true;
}

if (
isVariableDeclaration(node) &&
isSideEffectInVariableDeclaration(node)
) {
return true;
}

const expressionIdentifier = getPropertyIdentifierNode(node);

if (!expressionIdentifier) {
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/no-wait-for-side-effects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,5 +795,25 @@ ruleTester.run(RULE_NAME, rule, {
{ line: 12, column: 13, messageId: 'noSideEffectsWaitFor' },
],
},
// side effects (userEvent, fireEvent or render) in variable declarations
...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
{
// Issue #368, https://github.com/testing-library/eslint-plugin-testing-library/issues/368
code: `
import { waitFor } from '${testingFramework}';
import userEvent from '@testing-library/user-event'
await waitFor(() => {
const a = userEvent.click(button);
const b = fireEvent.click(button);
const wrapper = render(<App />);
})
`,
errors: [
{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' },
{ line: 6, column: 11, messageId: 'noSideEffectsWaitFor' },
{ line: 7, column: 11, messageId: 'noSideEffectsWaitFor' },
],
} as const,
]),
],
});