@@ -120,6 +120,40 @@ export default createTestingLibraryRule<Options, MessageIds>({
120
120
return node . expressions . some ( isRenderInAssignmentExpression ) ;
121
121
}
122
122
123
+ /**
124
+ * Checks if there are side effects in variable declarations.
125
+ *
126
+ * For example, these variable declarations have side effects:
127
+ * const a = userEvent.doubleClick(button);
128
+ * const b = fireEvent.click(button);
129
+ * const wrapper = render(<Component />);
130
+ *
131
+ * @param node
132
+ * @returns {Boolean } Boolean indicating if variable declarataion has side effects
133
+ */
134
+ function isSideEffectInVariableDeclaration (
135
+ node : TSESTree . VariableDeclaration
136
+ ) : boolean {
137
+ return node . declarations . some ( ( declaration ) => {
138
+ if ( isCallExpression ( declaration . init ) ) {
139
+ const test = getPropertyIdentifierNode ( declaration . init ) ;
140
+
141
+ if ( ! test ) {
142
+ return false ;
143
+ }
144
+
145
+ return (
146
+ helpers . isFireEventUtil ( test ) ||
147
+ helpers . isUserEventUtil ( test ) ||
148
+ helpers . isRenderUtil ( test )
149
+ ) ;
150
+ }
151
+ return false ;
152
+ } ) ;
153
+
154
+ return false ;
155
+ }
156
+
123
157
function getSideEffectNodes (
124
158
body : TSESTree . Node [ ]
125
159
) : TSESTree . ExpressionStatement [ ] {
@@ -135,6 +169,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
135
169
return true ;
136
170
}
137
171
172
+ if (
173
+ isVariableDeclaration ( node ) &&
174
+ isSideEffectInVariableDeclaration ( node )
175
+ ) {
176
+ return true ;
177
+ }
178
+
138
179
const expressionIdentifier = getPropertyIdentifierNode ( node ) ;
139
180
140
181
if ( ! expressionIdentifier ) {
0 commit comments