Skip to content

fix(no-wait-for-side-effects): catch implicit arrow function return #352

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
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
55 changes: 42 additions & 13 deletions lib/rules/no-wait-for-side-effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
defaultOptions: [],
create: function (context, _, helpers) {
function isCallerWaitFor(
node: TSESTree.BlockStatement | TSESTree.CallExpression
): boolean {
if (!node.parent) {
return false;
}
const callExpressionNode = node.parent.parent as TSESTree.CallExpression;
const callExpressionIdentifier = getPropertyIdentifierNode(
callExpressionNode
);

return (
!!callExpressionIdentifier &&
helpers.isAsyncUtil(callExpressionIdentifier, ['waitFor'])
);
}

function getSideEffectNodes(
body: TSESTree.Node[]
): TSESTree.ExpressionStatement[] {
Expand All @@ -47,19 +64,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
}

function reportSideEffects(node: TSESTree.BlockStatement) {
if (!node.parent) {
return;
}
const callExpressionNode = node.parent.parent as TSESTree.CallExpression;
const callExpressionIdentifier = getPropertyIdentifierNode(
callExpressionNode
);

if (!callExpressionIdentifier) {
return;
}

if (!helpers.isAsyncUtil(callExpressionIdentifier, ['waitFor'])) {
if (!isCallerWaitFor(node)) {
return;
}

Expand All @@ -76,8 +81,32 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

function reportImplicitReturnSideEffect(node: TSESTree.CallExpression) {
if (!isCallerWaitFor(node)) {
return;
}

const expressionIdentifier = getPropertyIdentifierNode(node.callee);
if (!expressionIdentifier) {
return;
}

if (
!helpers.isFireEventUtil(expressionIdentifier) &&
!helpers.isUserEventUtil(expressionIdentifier)
) {
return;
}

context.report({
node,
messageId: 'noSideEffectsWaitFor',
});
}

return {
'CallExpression > ArrowFunctionExpression > BlockStatement': reportSideEffects,
'CallExpression > ArrowFunctionExpression > CallExpression': reportImplicitReturnSideEffect,
'CallExpression > FunctionExpression > BlockStatement': reportSideEffects,
};
},
Expand Down
48 changes: 46 additions & 2 deletions tests/lib/rules/no-wait-for-side-effects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,49 @@ ruleTester.run(RULE_NAME, rule, {
})
`,
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { waitFor } from 'somewhere-else';
await waitFor(() => fireEvent.keyDown(input, {key: 'ArrowDown'}))
`,
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { waitFor } from 'somewhere-else';
import { userEvent } from '@testing-library/react';
await waitFor(() => userEvent.click(button))
`,
},
{
settings: { 'testing-library/utils-module': '~/test-utils' },
code: `
import { waitFor, userEvent } from '~/test-utils';
await waitFor(() => userEvent.click(button))
`,
},
],
invalid: [
// fireEvent
{
code: `
import { waitFor } from '@testing-library/react';
import { waitFor } from '@testing-library/react';
await waitFor(() => fireEvent.keyDown(input, {key: 'ArrowDown'}))
`,
errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
},
{
settings: { 'testing-library/utils-module': '~/test-utils' },
code: `
import { waitFor, fireEvent } from '~/test-utils';
await waitFor(() => fireEvent.keyDown(input, {key: 'ArrowDown'}))
`,
errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
Expand Down Expand Up @@ -241,7 +278,14 @@ ruleTester.run(RULE_NAME, rule, {
// userEvent
{
code: `
import { waitFor } from '@testing-library/react';
import { waitFor } from '@testing-library/react';
await waitFor(() => userEvent.click(button))
`,
errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
userEvent.click(button)
})
Expand Down