testing-library / testing-library/eslint-plugin-testing-library
False positive for `no-node-access` when accessing variable declaration's property `children`, `activeElement`, `firstChild` etc
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 1.1k
- Forks
- 168
- Avg merge
- 15h 44m
- Merged PRs (30d)
- 14
Description
Have you read the Troubleshooting section?
Yes
Plugin version
v5.9.1
ESLint version
v8.23.0
Node.js version
16.10.0
package manager and version
npm 7.24.0
Operating system
macOS Big Sur, version 11.6
Bug description
Extracted to an issue of its own from discussion in #386.
Steps to reproduce
- Have a unit test with a variable declaration that has a
childrenproperty - Access that property in some part of the test (e.g.
expectstatement) - Run linter and see an error
For example, here is a test code snippet that produces the error:
import { render } from '@testing-library/react'; // Does not matter which library, this error happens with all
const navItemWithChildren = {
title: 'Cars',
children: [
{
title: 'Add car',
},
{
title: 'All cars',
},
],
};
render(<NavComponent itemWithChildren={navItemWithChildren} />)
expect(
screen.getByText(navItemWithChildren.children[0].title),
).toBeVisible();
Error output/screenshots
18:42 error Avoid direct Node access. Prefer using the methods from Testing Library testing-library/no-node-access
ESLint configuration
Recommend rules
Rule(s) affected
no-node-access
Anything else?
children and all other property names listed in PROPERTIES_RETURNING_NODES in lib/util/index.ts should not produce this error, if they happen to be properties of some variable declaration.
Do you want to submit a pull request to fix this bug?
No
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the no-node-access rule and PROPERTIES_RETURNING_NODES in lib/utils/index.ts, using the provided reproduction as the first case to lint. Confirm that properties such as children, activeElement, and firstChild on variable declarations no longer trigger the rule, while the existing direct-node-access cases still do.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- eslint, javascript, typescript
- Domain
- testing, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100