testing-library / testing-library/eslint-plugin-testing-library

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

Open
#673 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
1.1k
Forks
168
Avg merge
15h 44m
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/create-testing-library-rule/detect-testing-library-utils.ts at the isQuery helper, then review the related test cases from PR #703. Check how imports from react-test-renderer and @testing-library/react-native are identified, and verify that findByType, findByProps, findAll, findAllByType, and findAllByProps are ignored by all affected query rules.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react, react-native, typescript
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.