Skip to content

fix: add no-await-sync-events exception for userEvent.keyboard #283

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 4 commits into from
Mar 21, 2021
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
8 changes: 4 additions & 4 deletions lib/rules/no-await-sync-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
const memberExpression = node.parent as TSESTree.MemberExpression;
const methodNode = memberExpression.property as TSESTree.Identifier;
const callExpression = memberExpression.parent as TSESTree.CallExpression;
const withDelay = callExpression.arguments.length >= 3 &&
isObjectExpression(callExpression.arguments[2]) &&
callExpression.arguments[2].properties.some(
const lastArg = callExpression.arguments[callExpression.arguments.length - 1]
const withDelay = isObjectExpression(lastArg) &&
lastArg.properties.some(
property =>
isProperty(property) &&
isIdentifier(property.key) &&
property.key.name === 'delay'
);

if (!(node.name === 'userEvent' && methodNode.name === 'type' && withDelay)) {
if (!(node.name === 'userEvent' && ['type', 'keyboard'].includes(methodNode.name) && withDelay)) {
context.report({
node: methodNode,
messageId: 'noAwaitSyncEvents',
Expand Down
12 changes: 11 additions & 1 deletion tests/lib/rules/no-await-sync-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ ruleTester.run(RULE_NAME, rule, {
}
`,
},
{
code: `() => {
await userEvent.keyboard('foo', {delay: 1234})
}
`,
},
],

invalid: [
Expand All @@ -157,9 +163,13 @@ ruleTester.run(RULE_NAME, rule, {
import userEvent from '@testing-library/user-event';
test('should report sync event awaited', async() => {
await userEvent.type('foo', 'bar', {hello: 1234});
await userEvent.keyboard('foo', {hello: 1234});
});
`,
errors: [{ line: 4, messageId: 'noAwaitSyncEvents' },],
errors: [
{ line: 4, messageId: 'noAwaitSyncEvents' },
{ line: 5, messageId: 'noAwaitSyncEvents' },
],
}
],
});