diff --git a/lib/rules/no-node-access.ts b/lib/rules/no-node-access.ts index b0362886..9bc3a713 100644 --- a/lib/rules/no-node-access.ts +++ b/lib/rules/no-node-access.ts @@ -22,8 +22,6 @@ export const RULE_NAME = 'no-node-access'; export type MessageIds = 'noNodeAccess'; export type Options = [{ allowContainerFirstChild: boolean }]; -const userEventInstanceNames = new Set(); - export default createTestingLibraryRule({ name: RULE_NAME, meta: { @@ -62,6 +60,8 @@ export default createTestingLibraryRule({ ], create(context, [{ allowContainerFirstChild = false }], helpers) { + const userEventInstanceNames = new Set(); + function showErrorForNodeAccess(node: TSESTree.MemberExpression) { // This rule is so aggressive that can cause tons of false positives outside test files when Aggressive Reporting // 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({ userEventInstanceNames.add(id.name); } }, + AssignmentExpression(node: TSESTree.AssignmentExpression) { + if ( + ASTUtils.isIdentifier(node.left) && + isCallExpression(node.right) && + isMemberExpression(node.right.callee) && + ASTUtils.isIdentifier(node.right.callee.object) + ) { + const testingLibraryFn = resolveToTestingLibraryFn( + node.right, + context + ); + if ( + node.right.callee.object.name === testingLibraryFn?.local && + ASTUtils.isIdentifier(node.right.callee.property) && + node.right.callee.property.name === 'setup' + ) { + userEventInstanceNames.add(node.left.name); + } + } + }, 'ExpressionStatement MemberExpression': showErrorForNodeAccess, 'VariableDeclarator MemberExpression': showErrorForNodeAccess, }; diff --git a/package.json b/package.json index 639769de..576ecd80 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "is-ci": "^3.0.1", "jest": "^29.7.0", "lint-staged": "^15.2.10", - "prettier": "3.3.3", + "prettier": "3.6.2", "semantic-release": "^24.2.0", "semver": "^7.6.3", "ts-node": "^10.9.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a86fdd0c..2372bbba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -91,8 +91,8 @@ importers: specifier: ^15.2.10 version: 15.4.3 prettier: - specifier: 3.3.3 - version: 3.3.3 + specifier: 3.6.2 + version: 3.6.2 semantic-release: specifier: ^24.2.0 version: 24.2.6(typescript@5.7.2) @@ -2980,8 +2980,8 @@ packages: resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==} engines: {node: '>= 0.8.0'} - prettier@3.3.3: - resolution: {integrity: sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==} + prettier@3.6.2: + resolution: {integrity: sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==} engines: {node: '>=14'} hasBin: true @@ -6982,7 +6982,7 @@ snapshots: prelude-ls@1.2.1: {} - prettier@3.3.3: {} + prettier@3.6.2: {} pretty-format@29.7.0: dependencies: diff --git a/tests/lib/rules/no-node-access.test.ts b/tests/lib/rules/no-node-access.test.ts index eb5906d3..41c29b16 100644 --- a/tests/lib/rules/no-node-access.test.ts +++ b/tests/lib/rules/no-node-access.test.ts @@ -219,6 +219,43 @@ ruleTester.run(RULE_NAME, rule, { const buttonText = screen.getByText('submit'); userEvt.click(buttonText); + `, + }, + { + code: ` + import { screen } from '${testingFramework}'; + import userEvent from '@testing-library/user-event'; + + describe('Testing', () => { + let user; + + beforeEach(() => { + user = userEvent.setup(); + }); + + it('test 1', async () => { + await user.click(screen.getByRole('button')); + }); + }); + `, + }, + { + settings: { 'testing-library/utils-module': 'test-utils' }, + code: ` + // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited) + import { screen, userEvent } from 'test-utils'; + + describe('Testing', () => { + let user; + + beforeEach(() => { + user = userEvent.setup(); + }); + + it('test 1', async () => { + await user.click(screen.getByRole('button')); + }); + }); `, }, {