Skip to content

fix: support AssignmentExpression in userEvent setup detection #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export const RULE_NAME = 'no-node-access';
export type MessageIds = 'noNodeAccess';
export type Options = [{ allowContainerFirstChild: boolean }];

const userEventInstanceNames = new Set<string>();

export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
Expand Down Expand Up @@ -52,6 +50,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
],

create(context, [{ allowContainerFirstChild = false }], helpers) {
const userEventInstanceNames = new Set<string>();

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
Expand Down Expand Up @@ -144,6 +144,26 @@ export default createTestingLibraryRule<Options, MessageIds>({
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,
};
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,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'));
});
});
`,
},
{
Expand Down