5

I have followed –or tried to– several posts on how to do it, including the airbnb enzyme's guide for (separatedly) react-native and jest. (E.g: https://medium.com/@childsmaidment/testing-react-native-components-with-enzyme-d46bf735540#.6sxq10kgt, https://blog.callstack.io/unit-testing-react-native-with-the-new-jest-i-snapshots-come-into-play-68ba19b1b9fe#.4iqylmqh5 or How to use Jest with React Native)

But I keep getting lots of warnings (I have multiple set of concurrent tests) whenever I try to render (not mount, it crashes) any native component. Warnings are always about a native prop not being recognised.

Warning: Unknown props `focus`, `secureTextEntry` on <TextInput> tag. Remove these props from the element. in TextInput (created by TextInput) in TextInput (created by PasswordInput) 

Anyone who has a set up working, recognises how to remove the warning or how to solve it?

Thanks

1
  • Are you rendering <TextInput> in your own components ? Looks like you're passing some unrecognize props. Can you add your component's implementation as well as the test ? I could use a bit more context in order to help you ;) Commented Mar 20, 2017 at 22:35

3 Answers 3

5

So I know this is a bit old but I was having issues with Jest, Enzyme, and React Native and I found this post - hopefully this solution will help.

To start with - Enzyme doesn't support mounting React Native and only supports shallow rendering. This wasn't good enough for me as I needed end-to-end tests from the component to the api which lead me to react-native-mock-render. What this does is allow us to run react native inside a browser environment which let's us test using Enzyme - all the calls for React Native and the components work as you would expect.

To set this up you'll need to install JSDOM, react-native-mock-render, Enzyme 3.0+, and Jest 20.0.0+. And then inside your jest setup file (which is specified in your package.json) include the following code:

const { JSDOM } = require('jsdom'); const jsdom = new JSDOM(); const { window } = jsdom; function copyProps(src, target) { const props = Object.getOwnPropertyNames(src) .filter(prop => typeof target[prop] === 'undefined') .map(prop => Object.getOwnPropertyDescriptor(src, prop)); Object.defineProperties(target, props); } global.window = window; global.document = window.document; global.navigator = { userAgent: 'node.js', }; copyProps(window, global); // Setup adapter to work with enzyme 3.2.0 const Enzyme = require('enzyme'); const Adapter = require('enzyme-adapter-react-16'); Enzyme.configure({ adapter: new Adapter() }); // Ignore React Web errors when using React Native console.error = (message) => { return message; }; require('react-native-mock-render/mock'); 

And that's it - you're all setup to mount components in Enzyme and test them.

If you want to see a full sample check out react-native-mock-render-example. This is working with React 16, React Native 0.51, and Enzyme 3.2.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi I tried your solution but I get the error node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js: missing super() call in constructor. Do you know a way around this? I've heard others say it's a problem with babel. I'm using babel-preset-react-native v4.0.0. Cheers!
Well I'm assuming this has to do with super() not being called in some constructor inside jsdom. Can you list what version of jsdom you're using? For my .babelrc all I have is { presets: [ "react-native" ]}
@Antonio did you find a solution for your issue? I have the same error.
1

In order to unit test your component with jest you can use enzyme-to-json

npm install --save enzyme-to-json 

then your test would look like this:

import { shallow } from 'enzyme'; import { shallowToJson } from 'enzyme-to-json'; import MyComponent from './MyComponent'; it('should render component', => { expect(shallowToJson(shallow(<MyComponent />))).toMatchSnapshot(); }); 

1 Comment

I do this already, but i am more interested in navigating through component props.
0

I'm not sure regarding your case with react-native. I can share my case of using jest + enzyme with standard react.

When I needed to test some component and isolate it from others I used jest.mock, e.g.

jest.mock('../ComponentToBeMocked', () => { return () => null; }); 

Initially I found examples when the second argument (a function) should return just a string representing a name of the mocked component. But in that case I saw that distracting Unknown props warning.

1 Comment

The problem with react-native is that the DOM is not compatible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.