@@ -56,13 +56,38 @@ export default createTestingLibraryRule<Options, MessageIds>({
56
56
57
57
create ( context , [ options ] , helpers ) {
58
58
const { eventModules = VALID_EVENT_MODULES } = options ;
59
+ let hasDelayDeclarationOrAssignmentGTZero : boolean ;
59
60
60
61
// userEvent.type() and userEvent.keyboard() are exceptions, which returns a
61
62
// Promise. But it is only necessary to wait when delay option other than 0
62
63
// is specified. So this rule has a special exception for the case await:
63
64
// - userEvent.type(element, 'abc', {delay: 1234})
64
65
// - userEvent.keyboard('abc', {delay: 1234})
65
66
return {
67
+ VariableDeclaration ( node : TSESTree . VariableDeclaration ) {
68
+ // Case delay has been declared outside of call expression's arguments
69
+ // Let's save the info if it is greater than zero
70
+ hasDelayDeclarationOrAssignmentGTZero = node . declarations . some (
71
+ ( property ) =>
72
+ ASTUtils . isIdentifier ( property . id ) &&
73
+ property . id . name === 'delay' &&
74
+ isLiteral ( property . init ) &&
75
+ property . init . value &&
76
+ property . init . value > 0
77
+ ) ;
78
+ } ,
79
+ AssignmentExpression ( node : TSESTree . AssignmentExpression ) {
80
+ // Case delay has been assigned or re-assigned outside of call expression's arguments
81
+ // Let's save the info if it is greater than zero
82
+ if (
83
+ ASTUtils . isIdentifier ( node . left ) &&
84
+ node . left . name === 'delay' &&
85
+ isLiteral ( node . right ) &&
86
+ node . right . value !== null
87
+ ) {
88
+ hasDelayDeclarationOrAssignmentGTZero = node . right . value > 0 ;
89
+ }
90
+ } ,
66
91
'AwaitExpression > CallExpression' ( node : TSESTree . CallExpression ) {
67
92
const simulateEventFunctionIdentifier = getDeepestIdentifierNode ( node ) ;
68
93
@@ -91,7 +116,20 @@ export default createTestingLibraryRule<Options, MessageIds>({
91
116
92
117
const lastArg = node . arguments [ node . arguments . length - 1 ] ;
93
118
94
- const hasDelay =
119
+ // Checking if there's a delay property
120
+ // Note: delay's value may have declared or assigned somewhere else (as a variable declaration or as an assignment expression)
121
+ // or right after this (as a literal)
122
+ const hasDelayProperty =
123
+ isObjectExpression ( lastArg ) &&
124
+ lastArg . properties . some (
125
+ ( property ) =>
126
+ isProperty ( property ) &&
127
+ ASTUtils . isIdentifier ( property . key ) &&
128
+ property . key . name === 'delay'
129
+ ) ;
130
+
131
+ // In case delay's value has been declared as a literal
132
+ const hasDelayLiteralGTZero =
95
133
isObjectExpression ( lastArg ) &&
96
134
lastArg . properties . some (
97
135
( property ) =>
@@ -107,7 +145,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
107
145
108
146
if (
109
147
USER_EVENT_ASYNC_EXCEPTIONS . includes ( simulateEventFunctionName ) &&
110
- hasDelay
148
+ hasDelayProperty &&
149
+ ( hasDelayDeclarationOrAssignmentGTZero || hasDelayLiteralGTZero )
111
150
) {
112
151
return ;
113
152
}
0 commit comments