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

Unexpected snapshot written when using react-native v0.56.0 #1743

Closed
rgal75 opened this issue Aug 9, 2018 · 15 comments
Closed

Unexpected snapshot written when using react-native v0.56.0 #1743

rgal75 opened this issue Aug 9, 2018 · 15 comments

Comments

@rgal75
Copy link

rgal75 commented Aug 9, 2018

Describe the bug
Having upgraded the react-native dependency in an app from 0.55.4 to 0.56.0 caused shallow to produce a different snapshot. With 0.56.0 the snapshot only contains <Component> tags instead of specific <View>, <Text>, etc. tags.

To Reproduce
Steps to reproduce the behavior:

  1. Create an app with react-native init
  2. Add enzyme (and some more) as a dev dependency. The resulting package.json is
{
  "name": "MyApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.4.1",
    "react-native": "0.56.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-eslint": "^8.2.6",
    "babel-jest": "^23.4.2",
    "babel-preset-flow": "^6.23.0",
    "babel-preset-react-native": "4.0.0",
    "enzyme": "^3.4.1",
    "enzyme-adapter-react-16": "^1.2.0",
    "enzyme-to-json": "^3.3.4",
    "eslint": "4.19.1",
    "eslint-config-airbnb": "^17.0.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-babel": "^5.1.0",
    "eslint-plugin-flowtype": "^2.50.0",
    "eslint-plugin-import": "^2.13.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-prettier": "^2.6.2",
    "eslint-plugin-react": "7.9.1",
    "eslint-plugin-react-native": "3.2.0",
    "flow-bin": "^0.78.0",
    "flow-typed": "^2.5.1",
    "jest": "^23.4.2",
    "prettier": "^1.14.2",
    "react-dom": "^16.4.1",
    "react-test-renderer": "^16.4.2"
  },
  "jest": {
    "preset": "react-native",
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "setupFiles": [
      "<rootDir>/jest/setup.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native|lottie-react-native|expo|react-native-maps|react-native-svg|react-native-branch|native-base-shoutem-theme|react-native-easy-grid|react-native-drawer|react-native-vector-icons|react-native-keyboard-aware-scroll-view|react-navigation|react-native-safe-area-view|react-native-iphone-x-helper|native-base|@expo|react-native-scrollable-tab-view|react-native-i18n|react-native-material-textfield|react-navigation-drawer)/)"
    ]
  }
}
  1. Add tests (App.test.js):
import React from 'react';
import { shallow } from 'enzyme';
import App from './App';

it('renders correctly - enzime', () => {
  const wrapper = shallow(<App />);
  expect(wrapper).toMatchSnapshot();
});
  1. The snapshot (when using react-native v0.56.0) has only <Component> tags:
exports[`renders correctly - enzime 1`] = `
<Component
  style={
    Object {
      "alignItems": "center",
      "backgroundColor": "#F5FCFF",
      "flex": 1,
      "justifyContent": "center",
    }
  }
>
  <Component
    style={
      Object {
        "fontSize": 20,
        "margin": 10,
        "textAlign": "center",
      }
    }
    testId="text"
  >
    Hello!
  </Component>
</Component>
`;

Expected behavior
The snapshot should be similar to:

exports[`renders correctly - enzyme 1`] = `
<View
  style={
    Object {
      "alignItems": "center",
      "backgroundColor": "#F5FCFF",
      "flex": 1,
      "justifyContent": "center",
    }
  }
>
  <Text
    style={
      Object {
        "fontSize": 20,
        "margin": 10,
        "textAlign": "center",
      }
    }
    testId="text"
  >
    Hello!
  </Text>
</View>
`;

Screenshots
none

Desktop (please complete the following information):

  • OS: MacOS v10.13.6
  • Node v8.11.3
  • NPM v6.1.0
@ljharb
Copy link
Member

ljharb commented Aug 9, 2018

You'd need a react native adapter to reliably use enzyme with React Native (see #1436).

enzyme also doesn't support snapshots (but .debug() output should work well for that).

Can you share the output of .debug(), before and after the update?

@rgal75
Copy link
Author

rgal75 commented Aug 11, 2018

The test using .debug():

it("renders correctly", () => {
  const wrapper = shallow(<App />).debug();
  expect(wrapper).toMatchSnapshot();
});

With react-native 0.55.4 the snapshot is

exports[`renders correctly 1`] = `
"<Component style={{...}}>
  <Text style={{...}} accessible={true} allowFontScaling={true} ellipsizeMode=\\"tail\\">
    Hello!
  </Text>
</Component>"
`;

with react-native 0.56.0 it is

exports[`renders correctly`] = `
"<Component style={{...}}>
  <Component testId=\\"text\\" style={{...}}>
    Hello!
  </Component>
</Component>"
`;

@ljharb
Copy link
Member

ljharb commented Aug 11, 2018

I'm confused; if it's changing as react-native changes, that's an issue with react-native (and one a react-native adapter could handle). What's changing as enzyme changes?

@corintho
Copy link

Could this be related to #1539 ? The reflection from reconciler is out of sync with the dom generated from react-native?

@ljharb
Copy link
Member

ljharb commented Aug 17, 2018

@corintho it's entirely possible. Either way, using enzyme with react-native without a react-native-specific adapter is always going to have quirks.

@ljharb
Copy link
Member

ljharb commented Aug 17, 2018

Closing for now; happy to reopen if needed.

@ljharb ljharb closed this as completed Aug 17, 2018
@rajapanidepu
Copy link

@ljharb @rgal75 I am facing the same issue while using babel 7. What is the suggested fix here?

@ljharb
Copy link
Member

ljharb commented Sep 12, 2018

@rajapanidepu there's no fix I'm aware of; without a react-native adapter, enzyme doesn't currently support react-native. If someone were to create one - whether in this repo, or separately - then that would be the path for addressing any issues.

@AndrewHenderson
Copy link

AndrewHenderson commented Oct 4, 2018

@rgal75 I'm experiencing this same issue with react-test-renderer/shallow.

I began experiencing this after upgrading to MacOS Mojave for Xcode 10 for iOS 12 support. I was then forced to upgrade React and React Native for compatibility with Xcode 10.

screen shot 2018-10-04 at 1 02 31 pm

Apologies for posting in Enzyme repo, but this is the only ticket I've found regarding this issue. I'm hoping they are related.

Below is my package.json with new changes on the right side. Changes to the Dev Dependencies is the result of me hacking away at broken Jest tests after upgrading main dependencies.

screen shot 2018-10-04 at 1 08 38 pm

And this is my .babelrc with new changes on the right side, as well:

screen shot 2018-10-04 at 1 09 35 pm

@tjjjwxzq
Copy link

We're facing the exact same issue where calling wrapper.debug() shows that enzyme is rendering a Text as a Component instead.

Encountered this initially when trying to upgrade from RN 0.55 to 0.57. We thought it was due to the enzyme-adapter-react-16 only officially supporting up to react ^16.4.0.0, while RN 0.57 is already using react 16.5.x, so we tried a less ambitious upgrade to RN 0.56 (since that uses react 16.4.0) but the same issue occurs.

Do we have to hold off on upgrading RN entirely until enzyme comes up with a proper RN adapter? And is there even a roadmap for that? We will probably hold off upgrading for now but we really want to upgrade our RN version by the end of this year and we're not sure whether this issue will be properly resolved by then. Maybe we will have to end up switching to react-test-renderer, but at this point I'm not sure if the same issue would still occur and that would also require a huge overhaul in our existing test suite.

@ljharb
Copy link
Member

ljharb commented Oct 11, 2018

@tjjjwxzq it's unlikely we'll be able to devote any roadmap time to developing a react-native adapter, since airbnb no longer uses RN. However, if a community member submitted a PR, I'd be more than happy to help review, and update the branch when possible, to get it into a shippable state.

@tjjjwxzq
Copy link

@ljharb I see. Thanks for the quick response! 😃

@rajivshah3
Copy link

@tjjjwxzq @AndrewHenderson @rgal75 I think this might actually be an issue with React Native itself: facebook/react-native#21937

@jamesmcn1
Copy link

Still an issue for me :( - RN 0.57.0

@linnett
Copy link

linnett commented Feb 3, 2019

Also same issue for me RN 0.57.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants