Skip to content

Commit 19947ef

Browse files
authored
fix(no-node-access): skip reporting files without Testing Library import (#338)
* test(no-node-access): improve current invalid cases asserts * fix(no-node-access): skip Aggressive Reporting * refactor: simplify check for strict module imported Closes #334
1 parent 6868400 commit 19947ef

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed

lib/detect-testing-library-utils.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type GetTestingLibraryImportNodeFn = () => ImportModuleNode | null;
5656
type GetCustomModuleImportNodeFn = () => ImportModuleNode | null;
5757
type GetTestingLibraryImportNameFn = () => string | undefined;
5858
type GetCustomModuleImportNameFn = () => string | undefined;
59-
type IsTestingLibraryImportedFn = () => boolean;
59+
type IsTestingLibraryImportedFn = (isStrict?: boolean) => boolean;
6060
type IsGetQueryVariantFn = (node: TSESTree.Identifier) => boolean;
6161
type IsQueryQueryVariantFn = (node: TSESTree.Identifier) => boolean;
6262
type IsFindQueryVariantFn = (node: TSESTree.Identifier) => boolean;
@@ -242,11 +242,15 @@ export function detectTestingLibraryUtils<
242242
* then this method will return `true` ONLY IF a testing-library package
243243
* or custom module are imported.
244244
*/
245-
const isTestingLibraryImported: IsTestingLibraryImportedFn = () => {
245+
const isTestingLibraryImported: IsTestingLibraryImportedFn = (
246+
isStrict = false
247+
) => {
248+
const isSomeModuleImported =
249+
!!importedTestingLibraryNode || !!importedCustomModuleNode;
250+
246251
return (
247-
isAggressiveModuleReportingEnabled() ||
248-
!!importedTestingLibraryNode ||
249-
!!importedCustomModuleNode
252+
(!isStrict && isAggressiveModuleReportingEnabled()) ||
253+
isSomeModuleImported
250254
);
251255
};
252256

lib/rules/no-node-access.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
2323
},
2424
defaultOptions: [],
2525

26-
create(context) {
26+
create(context, _, helpers) {
2727
function showErrorForNodeAccess(node: TSESTree.MemberExpression) {
28+
// This rule is so aggressive that can cause tons of false positives outside test files when Aggressive Reporting
29+
// is enabled. Because of that, this rule will skip this mechanism and report only if some Testing Library package
30+
// or custom one (set in utils-module Shared Setting) is found.
31+
if (!helpers.isTestingLibraryImported(true)) {
32+
return;
33+
}
34+
2835
ASTUtils.isIdentifier(node.property) &&
2936
ALL_RETURNING_NODES.includes(node.property.name) &&
3037
context.report({

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

+49-23
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ruleTester.run(RULE_NAME, rule, {
3131
},
3232
{
3333
code: `
34-
import { screen } from '@testing-library/react';
34+
import { screen } from '@testing-library/react';
3535
3636
const { getByText } = screen;
3737
const button = getByRole('button');
@@ -43,29 +43,57 @@ ruleTester.run(RULE_NAME, rule, {
4343
import { render, within } from '@testing-library/react';
4444
4545
const { getByLabelText } = render(<MyComponent />);
46-
const signinModal = getByLabelText('Sign In');
47-
within(signinModal).getByPlaceholderText('Username');
46+
const signInModal = getByLabelText('Sign In');
47+
within(signInModal).getByPlaceholderText('Username');
4848
`,
4949
},
5050
{
5151
code: `
52-
// case: importing custom module
53-
const closestButton = document.getElementById('submit-btn').closest('button');
54-
expect(closestButton).toBeInTheDocument();
52+
// case: code not related to testing library at all
53+
ReactDOM.render(
54+
<CommProvider useDsa={false}>
55+
<ThemeProvider>
56+
<GlobalStyle />
57+
<Suspense fallback={<Loader />}>
58+
<AppLogin />
59+
</Suspense>
60+
</ThemeProvider>
61+
</CommProvider>,
62+
63+
document.getElementById('root')
64+
);
5565
`,
66+
},
67+
{
5668
settings: {
5769
'testing-library/utils-module': 'test-utils',
5870
},
71+
code: `
72+
// case: custom module set but not imported (aggressive reporting limited)
73+
const closestButton = document.getElementById('submit-btn').closest('button');
74+
expect(closestButton).toBeInTheDocument();
75+
`,
76+
},
77+
{
78+
code: `
79+
// case: without importing TL (aggressive reporting skipped)
80+
const closestButton = document.getElementById('submit-btn')
81+
expect(closestButton).toBeInTheDocument();
82+
`,
5983
},
6084
],
6185
invalid: [
6286
{
87+
settings: {
88+
'testing-library/utils-module': 'test-utils',
89+
},
6390
code: `
64-
// case: without importing TL (aggressive reporting)
91+
// case: importing from custom module (aggressive reporting limited)
92+
import 'test-utils';
6593
const closestButton = document.getElementById('submit-btn')
6694
expect(closestButton).toBeInTheDocument();
6795
`,
68-
errors: [{ messageId: 'noNodeAccess', line: 3 }],
96+
errors: [{ line: 4, column: 38, messageId: 'noNodeAccess' }],
6997
},
7098
{
7199
code: `
@@ -75,9 +103,13 @@ ruleTester.run(RULE_NAME, rule, {
75103
`,
76104
errors: [
77105
{
106+
line: 4,
107+
column: 33,
78108
messageId: 'noNodeAccess',
79109
},
80110
{
111+
line: 4,
112+
column: 62,
81113
messageId: 'noNodeAccess',
82114
},
83115
],
@@ -90,6 +122,8 @@ ruleTester.run(RULE_NAME, rule, {
90122
`,
91123
errors: [
92124
{
125+
line: 4,
126+
column: 18,
93127
messageId: 'noNodeAccess',
94128
},
95129
],
@@ -117,6 +151,8 @@ ruleTester.run(RULE_NAME, rule, {
117151
`,
118152
errors: [
119153
{
154+
line: 4,
155+
column: 43,
120156
messageId: 'noNodeAccess',
121157
},
122158
],
@@ -128,11 +164,7 @@ ruleTester.run(RULE_NAME, rule, {
128164
const { getByText } = render(<Example />)
129165
getByText('submit').closest('button');
130166
`,
131-
errors: [
132-
{
133-
messageId: 'noNodeAccess',
134-
},
135-
],
167+
errors: [{ line: 5, column: 29, messageId: 'noNodeAccess' }],
136168
},
137169
{
138170
code: `
@@ -165,11 +197,7 @@ ruleTester.run(RULE_NAME, rule, {
165197
const buttonText = screen.getByText('submit');
166198
const button = buttonText.closest('button');
167199
`,
168-
errors: [
169-
{
170-
messageId: 'noNodeAccess',
171-
},
172-
],
200+
errors: [{ line: 5, column: 35, messageId: 'noNodeAccess' }],
173201
},
174202
{
175203
code: `
@@ -181,6 +209,8 @@ ruleTester.run(RULE_NAME, rule, {
181209
`,
182210
errors: [
183211
{
212+
line: 6,
213+
column: 35,
184214
messageId: 'noNodeAccess',
185215
},
186216
],
@@ -192,11 +222,7 @@ ruleTester.run(RULE_NAME, rule, {
192222
const { getByText } = render(<Example />)
193223
const button = getByText('submit').closest('button');
194224
`,
195-
errors: [
196-
{
197-
messageId: 'noNodeAccess',
198-
},
199-
],
225+
errors: [{ line: 5, column: 44, messageId: 'noNodeAccess' }],
200226
},
201227
{
202228
code: `

0 commit comments

Comments
 (0)