eslint-plugin-testing-library: Library incorrectly report no-node-access when referencing props.children
Library complains Avoid direct Node access. Prefer using the methods from Testing Library.
for the following code props.children
:
I want to be able to insert children, or a default node.
return (
<>
{ // Other elements here. }
{'children' in props ? (
props.children
) : (
<MyComponent {...props} disabled={disabled} />
)}
</>
)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (4 by maintainers)
Commits related to this issue
- fix: props.children is not reported by no-node-access If node's object name is "props" any of the node accessing properties is not reported. Ref: #386 — committed to sjarva/eslint-plugin-testing-library by sjarva 2 years ago
- fix(no-node-access): document more valid test cases These examples were gotten from discussions in issue #386 — committed to testing-library/eslint-plugin-testing-library by sjarva 2 years ago
- test(no-node-access): add extra valid test case (#684) These examples were gotten from discussions in issue #386 — committed to testing-library/eslint-plugin-testing-library by sjarva 2 years ago
Hello, this is still an issue, even for simple objects that have
children
key. For example I have this object that I use in tests:and I use this object in assertion like this:
I get eslint error
41:44 error Avoid direct Node access. Prefer using the methods from Testing Library testing-library/no-node-access
This is totally a false positiveI have opened a PR (#658) that deals with the most common and asked for cases (asked by @Snapperfish, @JustFly1984, @SevenOutman and @scottstern).
Unfortunately, this PR does not fix the errors on a) destructured props (mentioned by @Belco90) …
or b) an object that has a property named
children
(example from @titenis but @thepuzzlemaster and @sterlingwalsh had a similar cases)I am somewhat sure that both of these remaining cases a) and b) can be resolved, but I don’t have all this code figured out yet. I’ll open new issue(s) about these when this issue is closed 📝
Turns out, that this code does not produce an error for this rule, so I haven’t opened an issue for this, but added it to valid unit tests for this rule (in #684 ).
I have opened issue #683 for this scenario.
Happy to take care of this in separate issues.
Any update on this issue? Same problem here when mocking components with jest.fn
I am running into the same issue in my test. I have a helper method which renders my component, of which i can pass in a partial props object to override any defaults the test is using. I am trying to reference the
children
property of a button element that is being created, but getting the warning, which seems unnecessary in this case.I’m just trying to avoid hard-coding the children string in multiple places in my test file.
I got same false negative in one of projects I work on. props.children should be valid. added ignore comment meanwhile.