Skip to content

fix(no-node-access): Avoid false positives when Testing Library is not imported #1042

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

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ export default createTestingLibraryRule<Options, MessageIds>({

return {
CallExpression(node: TSESTree.CallExpression) {
// 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
// or custom one (set in utils-module Shared Setting) is found.
if (!helpers.isTestingLibraryImported(true)) {
return;
}

const { callee } = node;
const property = isMemberExpression(callee) ? callee.property : null;
const object = isMemberExpression(callee) ? callee.object : null;
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
ruleTester.run(RULE_NAME, rule, {
valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap<RuleValidTestCase>(
(testingFramework) => [
{
// valid because no Testing Library module is imported
code: `
// case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
const body = document.body

body.click();
`,
},
{
code: `
import { screen } from '${testingFramework}';
Expand Down