Skip to content

Commit

Permalink
fix: exclude AND left from promise handled consideration
Browse files Browse the repository at this point in the history
  • Loading branch information
Chamion committed Mar 22, 2024
1 parent 8bd9d58 commit aa75e2e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/node-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,19 @@ function getRootExpression(
if (parent == null) return expression;
switch (parent.type) {
case AST_NODE_TYPES.ConditionalExpression:
case AST_NODE_TYPES.LogicalExpression:
return getRootExpression(parent);
case AST_NODE_TYPES.LogicalExpression:
switch (parent.operator) {
case '??':
case '||':
return getRootExpression(parent);
case '&&':
return parent.right === expression
? getRootExpression(parent)
: expression;
default:
return expression;

Check warning on line 269 in lib/node-utils/index.ts

View check run for this annotation

Codecov / codecov/patch

lib/node-utils/index.ts#L268-L269

Added lines #L268 - L269 were not covered by tests
}
case AST_NODE_TYPES.SequenceExpression:
return parent.expressions[parent.expressions.length - 1] === expression
? getRootExpression(parent)
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules/await-async-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ ruleTester.run(RULE_NAME, rule, {
await (null, userEvent.${eventMethod}(getByLabelText('username')));
await (condition ? null : userEvent.${eventMethod}(getByLabelText('username')));
await (condition && userEvent.${eventMethod}(getByLabelText('username')));
await (userEvent.${eventMethod}(getByLabelText('username')) || userEvent.${eventMethod}(getByLabelText('username')));
await (userEvent.${eventMethod}(getByLabelText('username')) ?? userEvent.${eventMethod}(getByLabelText('username')));
})
`,
options: [{ eventModule: 'userEvent' }] as const,
Expand Down Expand Up @@ -997,6 +999,32 @@ ruleTester.run(RULE_NAME, rule, {
test('unhandled expression that evaluates to promise is invalid', async () => {
condition ? null : (null, true && await userEvent.${eventMethod}(getByLabelText('username')));
});
`,
} as const)
),
...USER_EVENT_ASYNC_FUNCTIONS.map(
(eventMethod) =>
({
code: `
import userEvent from '${testingFramework}'
test('handled AND expression with left promise is invalid', async () => {
await (userEvent.${eventMethod}(getByLabelText('username')) && userEvent.${eventMethod}(getByLabelText('username')));
});
`,
errors: [
{
line: 4,
column: 11,
messageId: 'awaitAsyncEvent',
data: { name: eventMethod },
},
],
options: [{ eventModule: 'userEvent' }],
output: `
import userEvent from '${testingFramework}'
test('handled AND expression with left promise is invalid', async () => {
await (await userEvent.${eventMethod}(getByLabelText('username')) && userEvent.${eventMethod}(getByLabelText('username')));
});
`,
} as const)
),
Expand Down

0 comments on commit aa75e2e

Please sign in to comment.