@@ -56,13 +56,38 @@ export default createTestingLibraryRule<Options, MessageIds>({
5656
5757 create ( context , [ options ] , helpers ) {
5858 const { eventModules = VALID_EVENT_MODULES } = options ;
59+ let hasDelayDeclarationOrAssignmentGTZero : boolean ;
5960
6061 // userEvent.type() and userEvent.keyboard() are exceptions, which returns a
6162 // Promise. But it is only necessary to wait when delay option other than 0
6263 // is specified. So this rule has a special exception for the case await:
6364 // - userEvent.type(element, 'abc', {delay: 1234})
6465 // - userEvent.keyboard('abc', {delay: 1234})
6566 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+ } ,
6691 'AwaitExpression > CallExpression' ( node : TSESTree . CallExpression ) {
6792 const simulateEventFunctionIdentifier = getDeepestIdentifierNode ( node ) ;
6893
@@ -91,7 +116,20 @@ export default createTestingLibraryRule<Options, MessageIds>({
91116
92117 const lastArg = node . arguments [ node . arguments . length - 1 ] ;
93118
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 =
95133 isObjectExpression ( lastArg ) &&
96134 lastArg . properties . some (
97135 ( property ) =>
@@ -107,7 +145,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
107145
108146 if (
109147 USER_EVENT_ASYNC_EXCEPTIONS . includes ( simulateEventFunctionName ) &&
110- hasDelay
148+ hasDelayProperty &&
149+ ( hasDelayDeclarationOrAssignmentGTZero || hasDelayLiteralGTZero )
111150 ) {
112151 return ;
113152 }
0 commit comments