Skip to content

Commit 7510adf

Browse files
committed
feat: enhance no-node-access rule to support userEvent setup function in tests
1 parent bdc3506 commit 7510adf

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/rules/no-node-access.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,21 @@ export default createTestingLibraryRule<Options, MessageIds>({
121121
},
122122
VariableDeclarator(node: TSESTree.VariableDeclarator) {
123123
const { init, id } = node;
124+
125+
if (!isCallExpression(init)) {
126+
return;
127+
}
128+
129+
if (
130+
!isMemberExpression(init.callee) ||
131+
!ASTUtils.isIdentifier(init.callee.object)
132+
) {
133+
return;
134+
}
135+
136+
const testingLibraryFn = resolveToTestingLibraryFn(init, context);
124137
if (
125-
init &&
126-
isCallExpression(init) &&
127-
isMemberExpression(init.callee) &&
128-
ASTUtils.isIdentifier(init.callee.object) &&
129-
init.callee.object.name === 'userEvent' &&
138+
init.callee.object.name === testingLibraryFn?.local &&
130139
ASTUtils.isIdentifier(init.callee.property) &&
131140
init.callee.property.name === 'setup' &&
132141
ASTUtils.isIdentifier(id)

tests/lib/rules/no-node-access.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ ruleTester.run(RULE_NAME, rule, {
197197
import userEvt from '@testing-library/user-event';
198198
import { screen } from '${testingFramework}';
199199
200+
const buttonText = screen.getByText('submit');
201+
const userAlias = userEvt.setup();
202+
userAlias.click(buttonText);
203+
`,
204+
},
205+
{
206+
code: `
207+
import userEvt from '@testing-library/user-event';
208+
import { screen } from '${testingFramework}';
209+
200210
const buttonText = screen.getByText('submit');
201211
userEvt.click(buttonText);
202212
`,

0 commit comments

Comments
 (0)