question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

False positives for `find*` queries from `react-test-renderer`

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sjarvacommented, Oct 17, 2022

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 📝

0reactions
Belco90commented, Dec 26, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-test-renderer: promise returned from `findByProps` query ...
React (and react-test-renderer) version: 17.0.2 Eslint version: 8.6.0 ... I think the lint rule, in this case, is just a false positive ......
Read more >
Testing-library: avoid these mistakes in async tests
Indeed, for a user with an id "alice", our request should return the name "Alice". False-positive case. Now, let's see if our test...
Read more >
Test Renderer - React
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM...
Read more >
TDD with React Test Renderer - LogRocket Blog
Smart error tracking lets you triage and categorize issues, then learns from this. Get notified of impactful user issues, not false positives.
Read more >
React testing library, how to get text from element
Though be careful when using queryByText (or any other react-testing-library method that starts with query ), using that will return null ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found