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

Jest/Enzyme snapshots show 'Component' instead of the native component #21937

Closed
3 tasks done
rajivshah3 opened this issue Oct 25, 2018 · 9 comments
Closed
3 tasks done
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@rajivshah3
Copy link
Contributor

rajivshah3 commented Oct 25, 2018

Environment

React Native Environment Info:
    System:
      OS: macOS 10.14
      CPU: x64 Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
      Memory: 189.02 MB / 8.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 8.11.4 - /usr/local/bin/node
      Yarn: 1.10.1 - /usr/local/bin/yarn
      npm: 6.4.1 - ~/.config/yarn/global/node_modules/.bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        Build Tools: 27.0.3, 28.0.1, 28.0.3
        API Levels: 27, 28
    IDEs:
      Android Studio: 3.2 AI-181.5540.7.32.5056338
      Xcode: 10.1/10O23u - /usr/bin/xcodebuild
    npmPackages:
      react: 16.6.0-alpha.8af6728 => 16.6.0-alpha.8af6728 
      react-native: 0.57.3 => 0.57.3 

Description

In React Native 0.57, the snapshots taken by Jest and Enzyme show Component instead of the actual native component (e.g. View, Text). See diff. This is not only an issue when used with Jest, but also with Enzyme (enzymejs/enzyme#1743).

Reproducible Demo

This can be reproduced by simply upgrading react-native as shown here.

A full example is located at https://github.com/rajivshah3/rn-broken-snapshot. The master branch is the latest version of React Native with a working snapshot test, the 0.55 branch is v0.55 of React Native, and broken-example is an example of a project that is trying to upgrade from v0.55 to the latest version.

@rajivshah3 rajivshah3 changed the title Jest/Enzyme snapshots are broken Jest/Enzyme snapshots show 'Component' instead of the native component Oct 25, 2018
@rajivshah3
Copy link
Contributor Author

It seems like Button and Switch don't have this issue. Is it possible that e708010 or 3e534b9 caused this?

@ljharb
Copy link

ljharb commented Oct 25, 2018

Yes; you’d have to explicitly set a displayName property on the forwardRef to get it to show up.

@rajivshah3
Copy link
Contributor Author

Aha, thanks @ljharb ! It looks like they tried to do that but ran into a problem, so they removed it (
ddf2c2f#diff-a562e4a7ac9e900c6d2bc644453e9152). I think it wasn’t working because forwardRef seems to not like arrow functions:
facebook/react#13703 (comment). I might be able to do a PR to fix this

kelset pushed a commit that referenced this issue Nov 9, 2018
Summary:
Adds the displayName prop to `View` and `Text` components. Because these now use `React.forwardRef`, they were showing as `Component` instead of their actual names.

Thanks to ljharb for helping to pinpoint the source of the issue!

Fixes #21937
Pull Request resolved: #21950

Differential Revision: D12827060

Pulled By: TheSavior

fbshipit-source-id: d812cae14d53ad821ab5873e737db63ad1a989e3
facebook-github-bot pushed a commit that referenced this issue Nov 27, 2018
Summary:
Similar to #21950, this adds displayName to `ActivityIndicator`, so it displays the correct component name in snapshots instead of `Component`.

Fixes #21937 (a little bit more than it was already fixed).
Pull Request resolved: #22417

Differential Revision: D13205376

Pulled By: TheSavior

fbshipit-source-id: 21e71d4980d159a1f33d32d83346b48257ec21d4
grabbou pushed a commit that referenced this issue Dec 4, 2018
Summary:
Similar to #21950, this adds displayName to `ActivityIndicator`, so it displays the correct component name in snapshots instead of `Component`.

Fixes #21937 (a little bit more than it was already fixed).
Pull Request resolved: #22417

Differential Revision: D13205376

Pulled By: TheSavior

fbshipit-source-id: 21e71d4980d159a1f33d32d83346b48257ec21d4
facebook-github-bot pushed a commit that referenced this issue Dec 6, 2018
Summary:
This diff adds a custom matcher which will wrap a given component and shallow render it, snapshotting the result with the formatting users will see when they do the same.

I also update the ActivityIndicator test to use this new strategy, and add an additional check that the displayName is set.

For context see:
- #21950
- https://github.com/rajivshah3/rn-broken-snapshot/blob/broken-example/__tests__/App.spec.js#L3
- #21937

Reviewed By: TheSavior

Differential Revision: D13326689

fbshipit-source-id: 8ed2302a8aad12962fa2498a46d2502ccfb5c737
kelset pushed a commit that referenced this issue Dec 12, 2018
Summary:
Similar to #21950, this adds displayName to `ActivityIndicator`, so it displays the correct component name in snapshots instead of `Component`.

Fixes #21937 (a little bit more than it was already fixed).
Pull Request resolved: #22417

Differential Revision: D13205376

Pulled By: TheSavior

fbshipit-source-id: 21e71d4980d159a1f33d32d83346b48257ec21d4
grabbou pushed a commit that referenced this issue Dec 17, 2018
Summary:
This diff adds a custom matcher which will wrap a given component and shallow render it, snapshotting the result with the formatting users will see when they do the same.

I also update the ActivityIndicator test to use this new strategy, and add an additional check that the displayName is set.

For context see:
- #21950
- https://github.com/rajivshah3/rn-broken-snapshot/blob/broken-example/__tests__/App.spec.js#L3
- #21937

Reviewed By: TheSavior

Differential Revision: D13326689

fbshipit-source-id: 8ed2302a8aad12962fa2498a46d2502ccfb5c737
@jamesmcn1
Copy link

@rajivshah3 did your fix get released in a new RN version yet?

@jamesmcn1
Copy link

jamesmcn1 commented Jan 22, 2019

Seems to have fixed all my tests when I just upgraded RN from 0.57.0 ---> 0.57.8

Thanks guys :)

@rajivshah3
Copy link
Contributor Author

rajivshah3 commented Jan 22, 2019 via email

@linnett
Copy link
Contributor

linnett commented Feb 3, 2019

Still seeing this issue in 0.57.8. <Image> showing as <Component> in my debug. Any ideas?

@rajivshah3
Copy link
Contributor Author

rajivshah3 commented Feb 3, 2019

@linnett it looks like the same issue. They moved to React.forwardRef but didn't specify the displayName property (see the React docs for info about this):

Image = React.forwardRef(Image);

If you want to do a PR for this, I think specifying displayName under the line shown above should fix it. So it would look like:

Image = React.forwardRef(Image);
Image.displayName = 'Image';

That's essentially what I did to fix View and Text in 7a914fc

@linnett
Copy link
Contributor

linnett commented Feb 4, 2019

Good shout @rajivshah3 - PR: #23287

facebook-github-bot pushed a commit that referenced this issue Feb 4, 2019
…ts (#23287)

Summary:
As per #21937, Image `displayName` is not set. This means it comes through as `<Component>` in tests.

[General][fixed] - fix `displayName` for `Image`
Pull Request resolved: #23287

Differential Revision: D13941744

Pulled By: cpojer

fbshipit-source-id: eab161eee415ec4f7207efcd5c6e2a4bbe67dfe1
matt-oakes pushed a commit to matt-oakes/react-native that referenced this issue Feb 7, 2019
…ts (facebook#23287)

Summary:
As per facebook#21937, Image `displayName` is not set. This means it comes through as `<Component>` in tests.

[General][fixed] - fix `displayName` for `Image`
Pull Request resolved: facebook#23287

Differential Revision: D13941744

Pulled By: cpojer

fbshipit-source-id: eab161eee415ec4f7207efcd5c6e2a4bbe67dfe1
t-nanava pushed a commit to microsoft/react-native-macos that referenced this issue Jun 17, 2019
Summary:
Adds the displayName prop to `View` and `Text` components. Because these now use `React.forwardRef`, they were showing as `Component` instead of their actual names.

Thanks to ljharb for helping to pinpoint the source of the issue!

Fixes facebook#21937
Pull Request resolved: facebook#21950

Differential Revision: D12827060

Pulled By: TheSavior

fbshipit-source-id: d812cae14d53ad821ab5873e737db63ad1a989e3
t-nanava pushed a commit to microsoft/react-native-macos that referenced this issue Jun 17, 2019
Summary:
Similar to facebook#21950, this adds displayName to `ActivityIndicator`, so it displays the correct component name in snapshots instead of `Component`.

Fixes facebook#21937 (a little bit more than it was already fixed).
Pull Request resolved: facebook#22417

Differential Revision: D13205376

Pulled By: TheSavior

fbshipit-source-id: 21e71d4980d159a1f33d32d83346b48257ec21d4
t-nanava pushed a commit to microsoft/react-native-macos that referenced this issue Jun 17, 2019
Summary:
This diff adds a custom matcher which will wrap a given component and shallow render it, snapshotting the result with the formatting users will see when they do the same.

I also update the ActivityIndicator test to use this new strategy, and add an additional check that the displayName is set.

For context see:
- facebook#21950
- https://github.com/rajivshah3/rn-broken-snapshot/blob/broken-example/__tests__/App.spec.js#L3
- facebook#21937

Reviewed By: TheSavior

Differential Revision: D13326689

fbshipit-source-id: 8ed2302a8aad12962fa2498a46d2502ccfb5c737
@facebook facebook locked as resolved and limited conversation to collaborators Oct 29, 2019
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants