|
| 1 | +import { AST_NODE_TYPES } from '@typescript-eslint/utils'; |
| 2 | + |
| 3 | +import { createTestingLibraryRule } from '../create-testing-library-rule'; |
| 4 | + |
| 5 | +import type { TSESTree } from '@typescript-eslint/utils'; |
| 6 | + |
| 7 | +export const RULE_NAME = 'prefer-user-event-setup'; |
| 8 | + |
| 9 | +export type MessageIds = 'preferUserEventSetup'; |
| 10 | +export type Options = []; |
| 11 | + |
| 12 | +const USER_EVENT_PACKAGE = '@testing-library/user-event'; |
| 13 | +const USER_EVENT_NAME = 'userEvent'; |
| 14 | +const SETUP_METHOD_NAME = 'setup'; |
| 15 | + |
| 16 | +// All userEvent methods that should use setup() |
| 17 | +const USER_EVENT_METHODS = [ |
| 18 | + 'clear', |
| 19 | + 'click', |
| 20 | + 'copy', |
| 21 | + 'cut', |
| 22 | + 'dblClick', |
| 23 | + 'deselectOptions', |
| 24 | + 'hover', |
| 25 | + 'keyboard', |
| 26 | + 'pointer', |
| 27 | + 'paste', |
| 28 | + 'selectOptions', |
| 29 | + 'tripleClick', |
| 30 | + 'type', |
| 31 | + 'unhover', |
| 32 | + 'upload', |
| 33 | + 'tab', |
| 34 | +] as const; |
| 35 | + |
| 36 | +export default createTestingLibraryRule<Options, MessageIds>({ |
| 37 | + name: RULE_NAME, |
| 38 | + meta: { |
| 39 | + type: 'problem', |
| 40 | + docs: { |
| 41 | + description: |
| 42 | + 'Suggest using userEvent with setup() instead of direct methods', |
| 43 | + recommendedConfig: { |
| 44 | + dom: false, |
| 45 | + angular: false, |
| 46 | + react: false, |
| 47 | + vue: false, |
| 48 | + svelte: false, |
| 49 | + marko: false, |
| 50 | + }, |
| 51 | + }, |
| 52 | + messages: { |
| 53 | + preferUserEventSetup: |
| 54 | + 'Prefer using userEvent with setup() instead of direct {{method}}() call. Use: const user = userEvent.setup(); await user.{{method}}(...)', |
| 55 | + }, |
| 56 | + schema: [], |
| 57 | + }, |
| 58 | + defaultOptions: [], |
| 59 | + |
| 60 | + create(context) { |
| 61 | + // Track variables assigned from userEvent.setup() |
| 62 | + const userEventSetupVars = new Set<string>(); |
| 63 | + |
| 64 | + // Track functions that return userEvent.setup() instances |
| 65 | + const setupFunctions = new Map<string, Set<string>>(); |
| 66 | + |
| 67 | + // Track imported userEvent identifier (could be aliased) |
| 68 | + let userEventIdentifier: string | null = null; |
| 69 | + |
| 70 | + function isUserEventSetupCall(node: TSESTree.Node): boolean { |
| 71 | + return ( |
| 72 | + node.type === AST_NODE_TYPES.CallExpression && |
| 73 | + node.callee.type === AST_NODE_TYPES.MemberExpression && |
| 74 | + node.callee.object.type === AST_NODE_TYPES.Identifier && |
| 75 | + node.callee.object.name === userEventIdentifier && |
| 76 | + node.callee.property.type === AST_NODE_TYPES.Identifier && |
| 77 | + node.callee.property.name === SETUP_METHOD_NAME |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + function isDirectUserEventMethodCall( |
| 82 | + node: TSESTree.MemberExpression |
| 83 | + ): boolean { |
| 84 | + return ( |
| 85 | + node.object.type === AST_NODE_TYPES.Identifier && |
| 86 | + node.object.name === userEventIdentifier && |
| 87 | + node.property.type === AST_NODE_TYPES.Identifier && |
| 88 | + USER_EVENT_METHODS.includes( |
| 89 | + node.property.name as (typeof USER_EVENT_METHODS)[number] |
| 90 | + ) |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + // Track userEvent imports |
| 96 | + ImportDeclaration(node: TSESTree.ImportDeclaration) { |
| 97 | + if (node.source.value === USER_EVENT_PACKAGE) { |
| 98 | + // Default import: import userEvent from '@testing-library/user-event' |
| 99 | + const defaultImport = node.specifiers.find( |
| 100 | + (spec) => spec.type === AST_NODE_TYPES.ImportDefaultSpecifier |
| 101 | + ); |
| 102 | + if (defaultImport) { |
| 103 | + userEventIdentifier = defaultImport.local.name; |
| 104 | + } |
| 105 | + |
| 106 | + // Named import: import { userEvent } from '@testing-library/user-event' |
| 107 | + const namedImport = node.specifiers.find( |
| 108 | + (spec) => |
| 109 | + spec.type === AST_NODE_TYPES.ImportSpecifier && |
| 110 | + spec.imported.type === AST_NODE_TYPES.Identifier && |
| 111 | + spec.imported.name === USER_EVENT_NAME |
| 112 | + ); |
| 113 | + if ( |
| 114 | + namedImport && |
| 115 | + namedImport.type === AST_NODE_TYPES.ImportSpecifier |
| 116 | + ) { |
| 117 | + userEventIdentifier = namedImport.local.name; |
| 118 | + } |
| 119 | + } |
| 120 | + }, |
| 121 | + |
| 122 | + // Track variables assigned from userEvent.setup() |
| 123 | + VariableDeclarator(node: TSESTree.VariableDeclarator) { |
| 124 | + if (!userEventIdentifier || !node.init) return; |
| 125 | + |
| 126 | + // Direct assignment: const user = userEvent.setup() |
| 127 | + if ( |
| 128 | + isUserEventSetupCall(node.init) && |
| 129 | + node.id.type === AST_NODE_TYPES.Identifier |
| 130 | + ) { |
| 131 | + userEventSetupVars.add(node.id.name); |
| 132 | + } |
| 133 | + |
| 134 | + // Destructuring from a setup function |
| 135 | + if ( |
| 136 | + node.id.type === AST_NODE_TYPES.ObjectPattern && |
| 137 | + node.init.type === AST_NODE_TYPES.CallExpression && |
| 138 | + node.init.callee.type === AST_NODE_TYPES.Identifier |
| 139 | + ) { |
| 140 | + const functionName = node.init.callee.name; |
| 141 | + const setupProps = setupFunctions.get(functionName); |
| 142 | + |
| 143 | + if (setupProps) { |
| 144 | + for (const prop of node.id.properties) { |
| 145 | + if ( |
| 146 | + prop.type === AST_NODE_TYPES.Property && |
| 147 | + prop.key.type === AST_NODE_TYPES.Identifier && |
| 148 | + setupProps.has(prop.key.name) && |
| 149 | + prop.value.type === AST_NODE_TYPES.Identifier |
| 150 | + ) { |
| 151 | + userEventSetupVars.add(prop.value.name); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + }, |
| 157 | + |
| 158 | + // Track functions that return objects with userEvent.setup() |
| 159 | + // Note: This simplified implementation only checks direct return statements |
| 160 | + // in the function body, not nested functions or complex flows |
| 161 | + FunctionDeclaration(node: TSESTree.FunctionDeclaration) { |
| 162 | + if (!userEventIdentifier || !node.id) return; |
| 163 | + |
| 164 | + // For simplicity, only check direct return statements in the function body |
| 165 | + if (node.body && node.body.type === AST_NODE_TYPES.BlockStatement) { |
| 166 | + for (const statement of node.body.body) { |
| 167 | + if (statement.type === AST_NODE_TYPES.ReturnStatement) { |
| 168 | + const ret = statement; |
| 169 | + if ( |
| 170 | + ret.argument && |
| 171 | + ret.argument.type === AST_NODE_TYPES.ObjectExpression |
| 172 | + ) { |
| 173 | + const props = new Set<string>(); |
| 174 | + for (const prop of ret.argument.properties) { |
| 175 | + if ( |
| 176 | + prop.type === AST_NODE_TYPES.Property && |
| 177 | + prop.key.type === AST_NODE_TYPES.Identifier && |
| 178 | + prop.value && |
| 179 | + isUserEventSetupCall(prop.value) |
| 180 | + ) { |
| 181 | + props.add(prop.key.name); |
| 182 | + } |
| 183 | + } |
| 184 | + if (props.size > 0) { |
| 185 | + setupFunctions.set(node.id.name, props); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + }, |
| 192 | + |
| 193 | + // Check for direct userEvent method calls |
| 194 | + CallExpression(node: TSESTree.CallExpression) { |
| 195 | + if (!userEventIdentifier) return; |
| 196 | + |
| 197 | + if ( |
| 198 | + node.callee.type === AST_NODE_TYPES.MemberExpression && |
| 199 | + isDirectUserEventMethodCall(node.callee) |
| 200 | + ) { |
| 201 | + const methodName = (node.callee.property as TSESTree.Identifier).name; |
| 202 | + |
| 203 | + // Check if this is called on a setup instance |
| 204 | + const isSetupInstance = |
| 205 | + node.callee.object.type === AST_NODE_TYPES.Identifier && |
| 206 | + userEventSetupVars.has(node.callee.object.name); |
| 207 | + |
| 208 | + if (!isSetupInstance) { |
| 209 | + context.report({ |
| 210 | + node: node.callee, |
| 211 | + messageId: 'preferUserEventSetup', |
| 212 | + data: { |
| 213 | + method: methodName, |
| 214 | + }, |
| 215 | + }); |
| 216 | + } |
| 217 | + } |
| 218 | + }, |
| 219 | + }; |
| 220 | + }, |
| 221 | +}); |
0 commit comments