Skip to content

Commit 7575464

Browse files
fix(no-await-sync-events) add exception for userEvent.keyboard (#283)
* Update no-await-sync-events.ts * refactor: use includes Co-authored-by: Michaël De Boey <[email protected]> * test: add tests for userEvent.keyboard * fix: check last arg for delay Co-authored-by: Michaël De Boey <[email protected]>
1 parent 1b4ea04 commit 7575464

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/rules/no-await-sync-events.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
3535
const memberExpression = node.parent as TSESTree.MemberExpression;
3636
const methodNode = memberExpression.property as TSESTree.Identifier;
3737
const callExpression = memberExpression.parent as TSESTree.CallExpression;
38-
const withDelay = callExpression.arguments.length >= 3 &&
39-
isObjectExpression(callExpression.arguments[2]) &&
40-
callExpression.arguments[2].properties.some(
38+
const lastArg = callExpression.arguments[callExpression.arguments.length - 1]
39+
const withDelay = isObjectExpression(lastArg) &&
40+
lastArg.properties.some(
4141
property =>
4242
isProperty(property) &&
4343
isIdentifier(property.key) &&
4444
property.key.name === 'delay'
4545
);
4646

47-
if (!(node.name === 'userEvent' && methodNode.name === 'type' && withDelay)) {
47+
if (!(node.name === 'userEvent' && ['type', 'keyboard'].includes(methodNode.name) && withDelay)) {
4848
context.report({
4949
node: methodNode,
5050
messageId: 'noAwaitSyncEvents',

tests/lib/rules/no-await-sync-events.test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ ruleTester.run(RULE_NAME, rule, {
138138
}
139139
`,
140140
},
141+
{
142+
code: `() => {
143+
await userEvent.keyboard('foo', {delay: 1234})
144+
}
145+
`,
146+
},
141147
],
142148

143149
invalid: [
@@ -157,9 +163,13 @@ ruleTester.run(RULE_NAME, rule, {
157163
import userEvent from '@testing-library/user-event';
158164
test('should report sync event awaited', async() => {
159165
await userEvent.type('foo', 'bar', {hello: 1234});
166+
await userEvent.keyboard('foo', {hello: 1234});
160167
});
161168
`,
162-
errors: [{ line: 4, messageId: 'noAwaitSyncEvents' },],
169+
errors: [
170+
{ line: 4, messageId: 'noAwaitSyncEvents' },
171+
{ line: 5, messageId: 'noAwaitSyncEvents' },
172+
],
163173
}
164174
],
165175
});

0 commit comments

Comments
 (0)