Skip to content

fix(no-wait-for-side-effects): false positive inside .then() #645

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/rules/no-wait-for-side-effects.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ Examples of **correct** code for this rule:
expect(b).toEqual('b');
});

// or
userEvent.click(button);
waitFor(function() {
expect(b).toEqual('b');
}).then(() => {
// Outside of waitFor, e.g. inside a .then() side effects are allowed
fireEvent.click(button);
});

// or
render(<App />)
await waitFor(() => {
Expand Down
21 changes: 21 additions & 0 deletions lib/rules/no-wait-for-side-effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isAssignmentExpression,
isCallExpression,
isSequenceExpression,
hasThenProperty,
} from '../node-utils';

export const RULE_NAME = 'no-wait-for-side-effects';
Expand Down Expand Up @@ -56,6 +57,22 @@ export default createTestingLibraryRule<Options, MessageIds>({
);
}

function isCallerThen(
node:
| TSESTree.AssignmentExpression
| TSESTree.BlockStatement
| TSESTree.CallExpression
| TSESTree.SequenceExpression
): boolean {
if (!node.parent) {
return false;
}

const callExpressionNode = node.parent.parent as TSESTree.CallExpression;

return hasThenProperty(callExpressionNode.callee);
}

function isRenderInVariableDeclaration(node: TSESTree.Node) {
return (
isVariableDeclaration(node) &&
Expand Down Expand Up @@ -137,6 +154,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

if (isCallerThen(node)) {
return;
}

getSideEffectNodes(node.body).forEach((sideEffectNode) =>
context.report({
node: sideEffectNode,
Expand Down
48 changes: 48 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 @@ -99,6 +99,19 @@ ruleTester.run(RULE_NAME, rule, {
await waitFor(function() {
expect(b).toEqual('b')
})
`,
},
{
// Issue #500, https://github.com/testing-library/eslint-plugin-testing-library/issues/500
code: `
import { waitFor } from '${testingFramework}';
userEvent.click(button)
waitFor(function() {
expect(b).toEqual('b')
}).then(() => {
// Side effects are allowed inside .then()
userEvent.click(button);
})
`,
},
]),
Expand Down Expand Up @@ -722,6 +735,41 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
} as const,
{
// Issue #500, https://github.com/testing-library/eslint-plugin-testing-library/issues/500
code: `
import { waitFor } from '${testingFramework}';
waitFor(function() {
userEvent.click(button)
expect(b).toEqual('b')
}).then(() => {
userEvent.click(button) // Side effects are allowed inside .then()
expect(b).toEqual('b')
})
`,
errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
} as const,
{
// Issue #500, https://github.com/testing-library/eslint-plugin-testing-library/issues/500
code: `
import { waitFor } from '${testingFramework}';
waitFor(function() {
userEvent.click(button)
expect(b).toEqual('b')
}).then(() => {
userEvent.click(button) // Side effects are allowed inside .then()
expect(b).toEqual('b')
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'}) // But not if there is a another waitFor with side effects inside the .then()
expect(b).toEqual('b')
})
})
`,
errors: [
{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' },
{ line: 10, column: 13, messageId: 'noSideEffectsWaitFor' },
],
} as const,
]),

{
Expand Down