Skip to content
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

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

Open
Belco90 opened this issue Oct 11, 2022 · 9 comments
Open

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

Belco90 opened this issue Oct 11, 2022 · 9 comments
Labels
bug Something isn't working

Comments

@Belco90
Copy link
Member

Belco90 commented Oct 11, 2022

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

@Belco90 Belco90 added bug Something isn't working triage Pending to be triaged by a maintainer labels Oct 11, 2022
@Mrblackey
Copy link

Couldn't we just get away with explicitly excluding /^find(All)?By(Type|Props)$/?

@Belco90 Belco90 removed the triage Pending to be triaged by a maintainer label Oct 11, 2022
@Belco90
Copy link
Member Author

Belco90 commented Oct 11, 2022

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.

@sjarva
Copy link
Collaborator

sjarva commented 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 📝

@Mrblackey
Copy link

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 📝

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?

@sjarva
Copy link
Collaborator

sjarva commented Oct 18, 2022

Indeed. Does this function already exist?

No, not yet.

@Belco90 Belco90 changed the title False positives for findBy* queries from react-test-renderer False positives for find* queries from react-test-renderer Dec 14, 2022
@Belco90 Belco90 changed the title False positives for find* queries from react-test-renderer False positives for find* queries from react-test-renderer Dec 14, 2022
@Belco90
Copy link
Member Author

Belco90 commented Dec 14, 2022

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.

@Belco90 Belco90 self-assigned this Dec 23, 2022
@Belco90
Copy link
Member Author

Belco90 commented Dec 23, 2022

Taking care of this one!

@Belco90
Copy link
Member Author

Belco90 commented 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.

@abejfehr
Copy link

abejfehr commented Mar 30, 2023

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',
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants