Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
60 changes: 47 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,28 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
defaultOptions: [],
create: function (context, _, helpers) {
function isCallerWaitFor(
node: TSESTree.BlockStatement | TSESTree.CallExpression
) {
if (!node.parent) {
return false;
}
const callExpressionNode = node.parent.parent as TSESTree.CallExpression;
const callExpressionIdentifier = getPropertyIdentifierNode(
callExpressionNode
);

if (!callExpressionIdentifier) {
return false;
}

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

return true;
}

function getSideEffectNodes(
body: TSESTree.Node[]
): TSESTree.ExpressionStatement[] {
Expand All @@ -47,19 +69,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 +86,32 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

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

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

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
18 changes: 16 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 @@ -163,7 +163,14 @@ ruleTester.run(RULE_NAME, rule, {
// 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' }],
},
{
code: `
import { waitFor } from '@testing-library/react';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
Expand Down Expand Up @@ -241,7 +248,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