False positives for `find*` queries from `react-test-renderer`
Have you read the Troubleshooting section?
Yes
Plugin version
v5.7.2
ESLint version
v8.25.0
Node.js version
16.15.0
package manager and version
n/a
Operating system
n/a
Bug description
The react-test-renderer library provides several utils which match the find* query pattern: findByType, findByProps, findAll, findAllByType, findAllByProps.
These methods are provided from a react-test-renderer instance, or any React Native Testing Library query. They should be ignored, so they are not treated as Testing Library queries (since they actually aren't).
This should apply to all rules, so it must be implemented in the isQuery internal helper. This function should ignore the queries listed before if they were imported from react-test-renderer or @testing-library/react-native. Checking the imports from the latter it's important since react-native-testing-library queries return nodes with the mentioned utils, so it's possible to do something like getByRole('modal').findByProps('bar'), hence ignoring react-test-renderer utils if coming from @testing-library/react-native
Steps to reproduce
This example extracted from React docs to cover the react-test-renderer import:
import TestRenderer from 'react-test-renderer';
function MyComponent() {
return (
<div>
<SubComponent foo="bar" />
<p className="my">Hello</p>
</div>
)
}
function SubComponent() {
return (
<p className="sub">Sub</p>
);
}
const testRenderer = TestRenderer.create(<MyComponent />);
const testInstance = testRenderer.root;
expect(testInstance.findByType(SubComponent).props.foo).toBe('bar');
expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']);
Example to cover the @testing-library/react-native import:
import { render } from '@testing-library/react-native';
test('some test case', () => {
render(<SomeComponent />);
const falsePositive = screen.getByText('foo').findByProps({ testID: 'bar' })
})
It would be interesting to check the test cases added in this PR.
Error output/screenshots
findByType, findByProps, and other react-test-renderer utils are reported by the plugin (e.g., await-async-queries complains about they must be awaited).
ESLint configuration
n/a
Rule(s) affected
All rules reporting on queries are affected, but await-async-queries is the most obvious one.
Anything else?
This was originated by #671 and #703
Do you want to submit a pull request to fix this bug?
No
Couldn't we just get away with explicitly excluding /^find(All)?By(Type|Props)$/?
Couldn't we just get away with explicitly excluding
/^find(All)?By(Type|Props)$/?
I'm afraid not because that would silence legit custom queries named like that.
Just making a note to whomever will fix this bug, there's another issue related to react-test-renderer: #494
and it will be handy to re-use the new helper function that detects imports from react-test-renderer in fixes to both issues 📝
Just making a note to whomever will fix this bug, there's another issue related to
react-test-renderer: #494 and it will be handy to re-use the new helper function that detects imports fromreact-test-rendererin fixes to both issues 📝
Indeed. Does this function already exist? Is it capable of tracking the origin of the act call if it is re-exported from another file, instead of directly from react-test-renderer?
Indeed. Does this function already exist?
No, not yet.
After @notjosh's clarification on #703 I'm repurposing this ticket, so we just exclude find* names coming from react-test-renderer as @Mrblackey originally suggested.
Taking care of this one!
I've closed my original PR #709 since such workaround could lead to other problems. I don't have enough time to implement the ideal fix, but I've updated the issue again with more details about how it should be done.
As a temporary workaround, @testing-library/react-native users can avoid the false positives by switching off the custom-queries reporting entirely.
I'll create a PR to mention this in the Troubleshooting section.
Sorry to be dense, but I'm seeing false positives for testing-library/await-async-query in React Native Testing Library with queries like screen.container.findByType(MyComponent), which seemingly does not return a Promise.
I've really tried following the Troubleshooting section mentioned in this comment, but I can't figure it out.
What exactly would I have to add in my eslint configuration to exempt findByType and findAllByType from this rule?
Edit: I got it, just do:
settings: {
'testing-library/custom-queries': 'off',
},