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.

react-test-renderer findAllByProps returns duplicate results for RN components

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment: OS: macOS Sierra 10.12.6 Node: 8.4.0 Yarn: 1.0.2 npm: 5.3.0 Watchman: 4.9.0 Xcode: Xcode 8.3.3 Build version 8E3004b Android Studio: 2.3 AI-162.4069837

Packages: (wanted => installed) react: ^16.0.0 => 16.0.0 react-native: ^0.49.3 => 0.49.3 react-test-renderer ^16.0.0 => 16.0.0

Target Platform: Node (testing using Jest)

Steps to Reproduce

(Reproducible repo below)

  1. Use react-test-renderer inside of a jest test to render a single View with a testID
  2. Use the renderer’s findAllByProps method to search for that testID

Expected Behavior

findAllByProps returns 1 instance, the View that was rendered

Actual Behavior

findAllByProps returns 2 instances, the View that was rendered and a child of that View with all of the same props.

Reproducible Demo

https://github.com/TAGraves/react-test-renderer-bug-example

Notes

I’m trying to use some of the new utilities provided by react-test-renderer to make assertions against a RN component hierarchy, but unfortunately the way RN mocks components when using Jest makes this a futile attempt. findAllByProps is returning twice as many elements as it should. I’ve attached a super simple example at the top (rendering just a single <View /> but getting two results from findAllByProps).

The issue stems from mockComponent.js. When it mocks a component out, its render returns another component with the same name and props. So if your intended tree looks like:

<View testID="a">
    <SomeComponent />
</View>

the outputted tree will be:

<View testID="a">
    <View testID="a">
      <SomeComponent />
    </View>
</View>

I believe this can be fixed by changing the mocked implementation just to:

const Component = class extends RealComponent {
  render() {
    return this.props.children || null;
  }
};

and I am happy to submit a PR with this change, but would like some guidance on if there’s a reason for the existing approach before I go changing things.

Thank you!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

1reaction
danielocampo2commented, Aug 18, 2018

Facing the same issue with latest version, quite frustrating indeed.

Did anybody find a workaround?

Edit: My workaround is instead to use findAll and filter also by type, like this:

const elements = component.root.findAll(n => n.props.testID === 'id' && n.type === 'Text')

Hope it helps.

0reactions
digitaldeuscommented, Aug 9, 2018

+1 I just encountered this issue as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Test Renderer: findAllByProps return double of what It ...
There is unresolved issue on react-native. Currently the only work-around is: const findAllByTestID = (instance) => instance.
Read more >
React Test Renderer: findAllByProps return double of what It ...
In my case I had been querying for components with a certain className . Typically the constructed component would have it's className made...
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
Learn how to use React Test Renderer to make specific assertions ... React Test Renderer has a great documentation, so I won't duplicate...
Read more >
Testing React Native components in Node with react ... - Medium
The react-test-renderer package makes it convenient to test components outside of their native environment (e.g. on an iOS/Android device for React Native ...
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