Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Latest commit

 

History

History
40 lines (26 loc) · 1.25 KB

react-native.md

File metadata and controls

40 lines (26 loc) · 1.25 KB

Using enzyme to Test Components in React Native

As of v0.18, React Native uses React as a dependency rather than a forked version of the library, which means it is now possible to use enzyme's shallow with React Native components.

Unfortunately, React Native has many environmental dependencies that can be hard to simulate without a host device.

This can be difficult when you want your test suite to run with typical Continuous Integration servers such as Travis.

A pure JS mock of React Native exists and can solve this problem in the majority of use cases.

To install it, run:

npm i --save-dev react-native-mock

Requiring or importing the /mock entry file of this project will input the mock react-native export into the require cache, so that your application uses the mock instead.

If you are using a test runner such as mocha, this means that you can use the --require flag before you run your test suite, and enzyme should "just work":

Mocha CLI

mocha --require react-native-mock/mock --recursive path/to/test/dir

In Code

/* file-that-runs-before-all-of-my-tests.js */

// This will mutate `react-native`'s require cache with `react-native-mock`'s.
require('react-native-mock/mock'); // <-- side-effects!!!