Skip to content

False positives for find* queries from react-test-renderer #673

Open
@Belco90

Description

@Belco90

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions