UNPKG

8.05 kBMarkdownView Raw
1Enzyme
2=======
3
4[![Join the chat at https://gitter.im/airbnb/enzyme](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/airbnb/enzyme?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5
6[![npm Version](https://img.shields.io/npm/v/enzyme.svg)](https://www.npmjs.com/package/enzyme) [![License](https://img.shields.io/npm/l/enzyme.svg)](https://www.npmjs.com/package/enzyme) [![Build Status](https://travis-ci.org/airbnb/enzyme.svg)](https://travis-ci.org/airbnb/enzyme) [![Coverage Status](https://coveralls.io/repos/airbnb/enzyme/badge.svg?branch=master&service=github)](https://coveralls.io/github/airbnb/enzyme?branch=master)
7
8
9Enzyme is a JavaScript Testing utility for React that makes it easier to test your React Components' output.
10You can also manipulate, traverse, and in some ways simulate runtime given the output.
11
12Enzyme's API is meant to be intuitive and flexible by mimicking jQuery's API for DOM manipulation
13and traversal.
14
15Upgrading from Enzyme 2.x or React < 16
16===========
17
18Are you here to check whether or not Enzyme is compatible with React 16? Are you currently using
19Enzyme 2.x? Great! Check out our [migration guide](/docs/guides/migration-from-2-to-3.md) for help
20moving on to Enzyme v3 where React 16 is supported.
21
22### [Installation](/docs/installation/README.md)
23
24To get started with enzyme, you can simply install it via npm. You will need to install enzyme
25along with an Adapter corresponding to the version of react (or other UI Component library) you
26are using. For instance, if you are using enzyme with React 16, you can run:
27
28```bash
29npm i --save-dev enzyme enzyme-adapter-react-16
30```
31
32Each adapter may have additional peer dependencies which you will need to install as well. For instance,
33`enzyme-adapter-react-16` has peer dependencies on `react` and `react-dom`.
34
35At the moment, Enzyme has adapters that provide compatibility with `React 16.x`, `React 15.x`,
36`React 0.14.x` and `React 0.13.x`.
37
38The following adapters are officially provided by enzyme, and have the following compatibility with
39React:
40
41| Enzyme Adapter Package | React semver compatibility |
42| --- | --- |
43| `enzyme-adapter-react-16` | `^16.4.0-0` |
44| `enzyme-adapter-react-16.3` | `~16.3.0-0` |
45| `enzyme-adapter-react-16.2` | `~16.2` |
46| `enzyme-adapter-react-16.1` | `~16.0.0-0 \|\| ~16.1` |
47| `enzyme-adapter-react-15` | `^15.5.0` |
48| `enzyme-adapter-react-15.4` | `15.0.0-0 - 15.4.x` |
49| `enzyme-adapter-react-14` | `^0.14.0` |
50| `enzyme-adapter-react-13` | `^0.13.0` |
51
52Finally, you need to configure enzyme to use the adapter you want it to use. To do this, you can use
53the top level `configure(...)` API.
54
55```js
56import Enzyme from 'enzyme';
57import Adapter from 'enzyme-adapter-react-16';
58
59Enzyme.configure({ adapter: new Adapter() });
60```
61
623rd Party Adapters
63=============
64
65It is possible for the community to create additional (non-official) adapters that will make enzyme
66work with other libraries. If you have made one and it's not included in the list below, feel free
67to make a PR to this README and add a link to it! The known 3rd party adapters are:
68
69| Adapter Package | For Library | Status |
70| --- | --- | --- |
71| [`preact-enzyme-adapter`](https://github.com/aweary/preact-enzyme-adapater) | [`preact`](https://github.com/developit/preact) | (work in progress) |
72|[`enzyme-adapter-inferno`](https://github.com/bbc/enzyme-adapter-inferno)|[`inferno`](https://github.com/infernojs/inferno)|(work in progress)|
73
74Running Enzyme Tests
75===========
76
77Enzyme is unopinionated regarding which test runner or assertion library you use, and should be
78compatible with all major test runners and assertion libraries out there. The documentation and
79examples for enzyme use [mocha](https://mochajs.org/) and [chai](http://chaijs.com/), but you
80should be able to extrapolate to your framework of choice.
81
82If you are interested in using enzyme with custom assertions and convenience functions for
83testing your React components, you can consider using:
84
85* [`chai-enzyme`](https://github.com/producthunt/chai-enzyme) with Mocha/Chai.
86* [`jasmine-enzyme`](https://github.com/FormidableLabs/enzyme-matchers/tree/master/packages/jasmine-enzyme) with Jasmine.
87* [`jest-enzyme`](https://github.com/FormidableLabs/enzyme-matchers/tree/master/packages/jest-enzyme) with Jest.
88* [`should-enzyme`](https://github.com/rkotze/should-enzyme) for should.js.
89* [`expect-enzyme`](https://github.com/PsychoLlama/expect-enzyme) for expect.
90
91
92[Using Enzyme with Mocha](/docs/guides/mocha.md)
93
94[Using Enzyme with Karma](/docs/guides/karma.md)
95
96[Using Enzyme with Browserify](/docs/guides/browserify.md)
97
98[Using Enzyme with SystemJS](/docs/guides/systemjs.md)
99
100[Using Enzyme with Webpack](/docs/guides/webpack.md)
101
102[Using Enzyme with JSDOM](/docs/guides/jsdom.md)
103
104[Using Enzyme with React Native](/docs/guides/react-native.md)
105
106[Using Enzyme with Jest](/docs/guides/jest.md)
107
108[Using Enzyme with Lab](/docs/guides/lab.md)
109
110[Using Enzyme with Tape and AVA](/docs/guides/tape-ava.md)
111
112Basic Usage
113===========
114
115## [Shallow Rendering](/docs/api/shallow.md)
116
117```javascript
118import React from 'react';
119import { expect } from 'chai';
120import { shallow } from 'enzyme';
121import sinon from 'sinon';
122
123import MyComponent from './MyComponent';
124import Foo from './Foo';
125
126describe('<MyComponent />', () => {
127 it('renders three <Foo /> components', () => {
128 const wrapper = shallow(<MyComponent />);
129 expect(wrapper.find(Foo)).to.have.lengthOf(3);
130 });
131
132 it('renders an `.icon-star`', () => {
133 const wrapper = shallow(<MyComponent />);
134 expect(wrapper.find('.icon-star')).to.have.lengthOf(1);
135 });
136
137 it('renders children when passed in', () => {
138 const wrapper = shallow((
139 <MyComponent>
140 <div className="unique" />
141 </MyComponent>
142 ));
143 expect(wrapper.contains(<div className="unique" />)).to.equal(true);
144 });
145
146 it('simulates click events', () => {
147 const onButtonClick = sinon.spy();
148 const wrapper = shallow(<Foo onButtonClick={onButtonClick} />);
149 wrapper.find('button').simulate('click');
150 expect(onButtonClick).to.have.property('callCount', 1);
151 });
152});
153```
154
155Read the full [API Documentation](/docs/api/shallow.md)
156
157
158
159## [Full DOM Rendering](/docs/api/mount.md)
160
161```javascript
162import React from 'react';
163import sinon from 'sinon';
164import { expect } from 'chai';
165import { mount } from 'enzyme';
166
167import Foo from './Foo';
168
169describe('<Foo />', () => {
170 it('allows us to set props', () => {
171 const wrapper = mount(<Foo bar="baz" />);
172 expect(wrapper.props().bar).to.equal('baz');
173 wrapper.setProps({ bar: 'foo' });
174 expect(wrapper.props().bar).to.equal('foo');
175 });
176
177 it('simulates click events', () => {
178 const onButtonClick = sinon.spy();
179 const wrapper = mount((
180 <Foo onButtonClick={onButtonClick} />
181 ));
182 wrapper.find('button').simulate('click');
183 expect(onButtonClick).to.have.property('callCount', 1);
184 });
185
186 it('calls componentDidMount', () => {
187 sinon.spy(Foo.prototype, 'componentDidMount');
188 const wrapper = mount(<Foo />);
189 expect(Foo.prototype.componentDidMount).to.have.property('callCount', 1);
190 Foo.prototype.componentDidMount.restore();
191 });
192});
193```
194
195Read the full [API Documentation](/docs/api/mount.md)
196
197
198## [Static Rendered Markup](/docs/api/render.md)
199
200```javascript
201import React from 'react';
202import { expect } from 'chai';
203import { render } from 'enzyme';
204
205import Foo from './Foo';
206
207describe('<Foo />', () => {
208 it('renders three `.foo-bar`s', () => {
209 const wrapper = render(<Foo />);
210 expect(wrapper.find('.foo-bar')).to.have.lengthOf(3);
211 });
212
213 it('renders the title', () => {
214 const wrapper = render(<Foo title="unique" />);
215 expect(wrapper.text()).to.contain('unique');
216 });
217});
218```
219
220Read the full [API Documentation](/docs/api/render.md)
221
222
223### Future
224
225[Enzyme Future](/docs/future.md)
226
227
228### Contributing
229
230See the [Contributors Guide](/CONTRIBUTING.md)
231
232### In the wild
233
234Organizations and projects using `enzyme` can list themselves [here](INTHEWILD.md).
235
236### License
237
238[MIT](/LICENSE.md)