Skip to content

testing-library/no-node-access reported twice #1256

@jonathan-dev

Description

@jonathan-dev

Have you read the Troubleshooting section?

Yes

Plugin version

v17.16.0

ESLint version

v10.0.3

Node.js version

v20.20.1

Bug description

The testing-library/no-node-access errors are reported twice when on the right side of an assignment.

/workspaces/no-node-access-bug/__test__/test.test.js
   1:10  error  'render' is defined but never used                                       no-unused-vars
   1:26  error  'fireEvent' is defined but never used                                    no-unused-vars
   3:1   error  'describe' is not defined                                                no-undef
   3:10  error  Strings must use doublequote                                             quotes
   4:5   error  'it' is not defined                                                      no-undef
   4:8   error  Strings must use doublequote                                             quotes
   6:15  error  'radioContainer' is assigned a value but never used                      no-unused-vars
   7:24  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   7:24  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   7:51  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   7:51  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   7:65  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   7:65  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   8:23  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   8:23  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
  10:9   error  'expect' is not defined                                                  no-undef

when the const radioContainer = in the test file is removed I only get one reporting.

/workspaces/no-node-access-bug/__test__/test.test.js
   1:10  error  'render' is defined but never used                                       no-unused-vars
   1:26  error  'fireEvent' is defined but never used                                    no-unused-vars
   3:1   error  'describe' is not defined                                                no-undef
   3:10  error  Strings must use doublequote                                             quotes
   4:5   error  'it' is not defined                                                      no-undef
   4:8   error  Strings must use doublequote                                             quotes
   7:1   error  Expected indentation of 8 spaces but found 12                            indent
   7:24  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   7:51  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   7:65  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
   8:23  error  Avoid direct Node access. Prefer using the methods from Testing Library  testing-library/no-node-access
  10:9   error  'expect' is not defined                                                  no-undef

Steps to reproduce

test file

import { render, screen, fireEvent } from "@testing-library/react";

describe('test', () => {
    it('should pass', () => {
        const rowHeader = screen.getByText(/Row Size:/i);
        const radioContainer =
            (rowHeader.parentElement && rowHeader.parentElement.nextElementSibling) ||
            rowHeader.parentElement;

        expect(true).toBe(true);
    });
});

eslint config

import testingLibrary from "eslint-plugin-testing-library";

import js from "@eslint/js";

export default [
    js.configs.recommended,
    {
        plugins: {
            "testing-library": testingLibrary,
        },
        rules: {
            // possible errors
            "no-console": "warn",
            "no-unused-vars": "error",

            // best practices
            "eqeqeq": "error",
            "no-eval": "error",
            "no-var": "error",
            "prefer-const": "error",
            "prefer-template": "warn",

            // style
            "semi": ["error", "always"],
            "quotes": ["error", "double"],
            "indent": ["error", 4],
            ...testingLibrary.configs["flat/react"].rules,
        },
    },
];

package.json

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "@eslint/eslintrc": "^3.3.5",
    "@eslint/js": "^10.0.1",
    "eslint": "^10.0.3",
    "eslint-plugin-testing-library": "^7.16.0",
    "jest": "^30.3.0"
  }
}

Error output/screenshots

No response

ESLint configuration

import testingLibrary from "eslint-plugin-testing-library";

import js from "@eslint/js";

export default [
    js.configs.recommended,
    {
        plugins: {
            "testing-library": testingLibrary,
        },
        rules: {
            // possible errors
            "no-console": "warn",
            "no-unused-vars": "error",

            // best practices
            "eqeqeq": "error",
            "no-eval": "error",
            "no-var": "error",
            "prefer-const": "error",
            "prefer-template": "warn",

            // style
            "semi": ["error", "always"],
            "quotes": ["error", "double"],
            "indent": ["error", 4],
            ...testingLibrary.configs["flat/react"].rules,
        },
    },
];

Rule(s) affected

testing-library/no-node-access

Anything else?

No response

Do you want to submit a pull request to fix this bug?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleasedtriagePending to be triaged by a maintainer

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions