Skip to content

fix(no-node-access): skip reporting files without Testing Library import #338

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

Merged
merged 3 commits into from
Apr 14, 2021
Merged
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
14 changes: 9 additions & 5 deletions lib/detect-testing-library-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type GetTestingLibraryImportNodeFn = () => ImportModuleNode | null;
type GetCustomModuleImportNodeFn = () => ImportModuleNode | null;
type GetTestingLibraryImportNameFn = () => string | undefined;
type GetCustomModuleImportNameFn = () => string | undefined;
type IsTestingLibraryImportedFn = () => boolean;
type IsTestingLibraryImportedFn = (isStrict?: boolean) => boolean;
type IsGetQueryVariantFn = (node: TSESTree.Identifier) => boolean;
type IsQueryQueryVariantFn = (node: TSESTree.Identifier) => boolean;
type IsFindQueryVariantFn = (node: TSESTree.Identifier) => boolean;
Expand Down Expand Up @@ -242,11 +242,15 @@ export function detectTestingLibraryUtils<
* then this method will return `true` ONLY IF a testing-library package
* or custom module are imported.
*/
const isTestingLibraryImported: IsTestingLibraryImportedFn = () => {
const isTestingLibraryImported: IsTestingLibraryImportedFn = (
isStrict = false
) => {
const isSomeModuleImported =
!!importedTestingLibraryNode || !!importedCustomModuleNode;

return (
isAggressiveModuleReportingEnabled() ||
!!importedTestingLibraryNode ||
!!importedCustomModuleNode
(!isStrict && isAggressiveModuleReportingEnabled()) ||
isSomeModuleImported
);
};

Expand Down
9 changes: 8 additions & 1 deletion lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
defaultOptions: [],

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

ASTUtils.isIdentifier(node.property) &&
ALL_RETURNING_NODES.includes(node.property.name) &&
context.report({
Expand Down
72 changes: 49 additions & 23 deletions tests/lib/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { screen } from '@testing-library/react';
import { screen } from '@testing-library/react';

const { getByText } = screen;
const button = getByRole('button');
Expand All @@ -43,29 +43,57 @@ ruleTester.run(RULE_NAME, rule, {
import { render, within } from '@testing-library/react';

const { getByLabelText } = render(<MyComponent />);
const signinModal = getByLabelText('Sign In');
within(signinModal).getByPlaceholderText('Username');
const signInModal = getByLabelText('Sign In');
within(signInModal).getByPlaceholderText('Username');
`,
},
{
code: `
// case: importing custom module
const closestButton = document.getElementById('submit-btn').closest('button');
expect(closestButton).toBeInTheDocument();
// case: code not related to testing library at all
ReactDOM.render(
<CommProvider useDsa={false}>
<ThemeProvider>
<GlobalStyle />
<Suspense fallback={<Loader />}>
<AppLogin />
</Suspense>
</ThemeProvider>
</CommProvider>,

document.getElementById('root')
);
`,
},
{
settings: {
'testing-library/utils-module': 'test-utils',
},
code: `
// case: custom module set but not imported (aggressive reporting limited)
const closestButton = document.getElementById('submit-btn').closest('button');
expect(closestButton).toBeInTheDocument();
`,
},
{
code: `
// case: without importing TL (aggressive reporting skipped)
const closestButton = document.getElementById('submit-btn')
expect(closestButton).toBeInTheDocument();
`,
},
],
invalid: [
{
settings: {
'testing-library/utils-module': 'test-utils',
},
code: `
// case: without importing TL (aggressive reporting)
// case: importing from custom module (aggressive reporting limited)
import 'test-utils';
const closestButton = document.getElementById('submit-btn')
expect(closestButton).toBeInTheDocument();
`,
errors: [{ messageId: 'noNodeAccess', line: 3 }],
errors: [{ line: 4, column: 38, messageId: 'noNodeAccess' }],
},
{
code: `
Expand All @@ -75,9 +103,13 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
line: 4,
column: 33,
messageId: 'noNodeAccess',
},
{
line: 4,
column: 62,
messageId: 'noNodeAccess',
},
],
Expand All @@ -90,6 +122,8 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
line: 4,
column: 18,
messageId: 'noNodeAccess',
},
],
Expand Down Expand Up @@ -117,6 +151,8 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
line: 4,
column: 43,
messageId: 'noNodeAccess',
},
],
Expand All @@ -128,11 +164,7 @@ ruleTester.run(RULE_NAME, rule, {
const { getByText } = render(<Example />)
getByText('submit').closest('button');
`,
errors: [
{
messageId: 'noNodeAccess',
},
],
errors: [{ line: 5, column: 29, messageId: 'noNodeAccess' }],
},
{
code: `
Expand Down Expand Up @@ -165,11 +197,7 @@ ruleTester.run(RULE_NAME, rule, {
const buttonText = screen.getByText('submit');
const button = buttonText.closest('button');
`,
errors: [
{
messageId: 'noNodeAccess',
},
],
errors: [{ line: 5, column: 35, messageId: 'noNodeAccess' }],
},
{
code: `
Expand All @@ -181,6 +209,8 @@ ruleTester.run(RULE_NAME, rule, {
`,
errors: [
{
line: 6,
column: 35,
messageId: 'noNodeAccess',
},
],
Expand All @@ -192,11 +222,7 @@ ruleTester.run(RULE_NAME, rule, {
const { getByText } = render(<Example />)
const button = getByText('submit').closest('button');
`,
errors: [
{
messageId: 'noNodeAccess',
},
],
errors: [{ line: 5, column: 44, messageId: 'noNodeAccess' }],
},
{
code: `
Expand Down