-
Notifications
You must be signed in to change notification settings - Fork 142
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
Library incorrectly report no-node-access when referencing props.children #386
Comments
Hey @Snapperfish, thanks for reporting this issue! For clarifying the issue: this happens on testing files too, so restricting the plugin to be run against test files only won't fix it. I think we should treat this as a bug then, but not really sure how to prevent it. The rule shouldn't report node accesses if coming from function args? There are still some cases that should be reported: // valid cases
function ComponentA(props) {
// this should NOT be reported
if (props.children) {
// ...
}
// this should NOT be reported
return <div>{props.children}</div>
}
function ComponentB({ children }) {
// this should NOT be reported
if (children) {
// ...
}
// this should NOT be reported
return <div>{children}</div>
} invalid cases
function assertElement(element) {
// this SHOULD be reported
expect(element.children).toBeDefined()
}
function getParagraphs({ children }) {
// this SHOULD be reported
return children.closest('p')
}
test('some test', () => {
const section = screen.getByRole('article')
assertElement(section)
const paragraphs = getParagraphs(section)
}); So I'm afraid just relying just on |
That could already be a start I think. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I definitely need to avoid stalebot to close issues with bug label 😅 |
Hello, this is still an issue, even for simple objects that have
and I use this object in assertion like this:
I get eslint error |
I got same false negative in one of projects I work on. props.children should be valid. added ignore comment meanwhile. |
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 I'm just trying to avoid hard-coding the children string in multiple places in my test file.
|
I have a data structure unrelated to any html element that just uses the word 'children' as a key on an object that I have a test for and this rule is being triggered. |
as a workaround you can use this code: const {children} = props |
Any update on this issue? Same problem here when mocking components with jest.fn jest.mock('@/features/auth', () => ({
Gate: jest.fn((props) => props.children), // Got "Avoid direct Node access. Prefer using the methods from Testing Library."
})); |
Having the same issue. destructuring children worked, but surprised this errored out |
No luck here in my TypeScript project. Have to deal with props declaration if using destructuring. :( |
If node's object name is "props" any of the node accessing properties is not reported. Ref: testing-library#386
I 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
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 📝 |
Happy to take care of this in separate issues. |
🎉 This issue has been resolved in version 5.7.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
These examples were gotten from discussions in issue #386
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. |
These examples were gotten from discussions in issue #386
Library complains
Avoid direct Node access. Prefer using the methods from Testing Library.
for the following codeprops.children
:I want to be able to insert children, or a default node.
See https://stackoverflow.com/q/67099835/481207
The text was updated successfully, but these errors were encountered: