Skip to content

Commit d428723

Browse files
authored
fix(no-node-access): improve user-event setup local detection (#1055)
Fixes #1057
1 parent 92a6bf5 commit d428723

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/rules/no-node-access.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export const RULE_NAME = 'no-node-access';
2222
export type MessageIds = 'noNodeAccess';
2323
export type Options = [{ allowContainerFirstChild: boolean }];
2424

25-
const userEventInstanceNames = new Set<string>();
26-
2725
export default createTestingLibraryRule<Options, MessageIds>({
2826
name: RULE_NAME,
2927
meta: {
@@ -62,6 +60,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
6260
],
6361

6462
create(context, [{ allowContainerFirstChild = false }], helpers) {
63+
const userEventInstanceNames = new Set<string>();
64+
6565
function showErrorForNodeAccess(node: TSESTree.MemberExpression) {
6666
// This rule is so aggressive that can cause tons of false positives outside test files when Aggressive Reporting
6767
// is enabled. Because of that, this rule will skip this mechanism and report only if some Testing Library package
@@ -169,6 +169,26 @@ export default createTestingLibraryRule<Options, MessageIds>({
169169
userEventInstanceNames.add(id.name);
170170
}
171171
},
172+
AssignmentExpression(node: TSESTree.AssignmentExpression) {
173+
if (
174+
ASTUtils.isIdentifier(node.left) &&
175+
isCallExpression(node.right) &&
176+
isMemberExpression(node.right.callee) &&
177+
ASTUtils.isIdentifier(node.right.callee.object)
178+
) {
179+
const testingLibraryFn = resolveToTestingLibraryFn(
180+
node.right,
181+
context
182+
);
183+
if (
184+
node.right.callee.object.name === testingLibraryFn?.local &&
185+
ASTUtils.isIdentifier(node.right.callee.property) &&
186+
node.right.callee.property.name === 'setup'
187+
) {
188+
userEventInstanceNames.add(node.left.name);
189+
}
190+
}
191+
},
172192
'ExpressionStatement MemberExpression': showErrorForNodeAccess,
173193
'VariableDeclarator MemberExpression': showErrorForNodeAccess,
174194
};

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,43 @@ ruleTester.run(RULE_NAME, rule, {
219219
220220
const buttonText = screen.getByText('submit');
221221
userEvt.click(buttonText);
222+
`,
223+
},
224+
{
225+
code: `
226+
import { screen } from '${testingFramework}';
227+
import userEvent from '@testing-library/user-event';
228+
229+
describe('Testing', () => {
230+
let user;
231+
232+
beforeEach(() => {
233+
user = userEvent.setup();
234+
});
235+
236+
it('test 1', async () => {
237+
await user.click(screen.getByRole('button'));
238+
});
239+
});
240+
`,
241+
},
242+
{
243+
settings: { 'testing-library/utils-module': 'test-utils' },
244+
code: `
245+
// case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
246+
import { screen, userEvent } from 'test-utils';
247+
248+
describe('Testing', () => {
249+
let user;
250+
251+
beforeEach(() => {
252+
user = userEvent.setup();
253+
});
254+
255+
it('test 1', async () => {
256+
await user.click(screen.getByRole('button'));
257+
});
258+
});
222259
`,
223260
},
224261
{

0 commit comments

Comments
 (0)