Enzyme

JavaScript Testing utilities for React

README

Enzyme
=======
Join the chat at https://gitter.im/enzymejs/enzyme
npm Version License Build Status Coverage Status


Enzyme is a JavaScript Testing utility for React that makes it easier to test your React Components' output.
You can also manipulate, traverse, and in some ways simulate runtime given the output.

Enzyme's API is meant to be intuitive and flexible by mimicking jQuery's API for DOM manipulation
and traversal.

Upgrading from Enzyme 2.x or React < 16
===========

Are you here to check whether or not Enzyme is compatible with React 16? Are you currently using
Enzyme 2.x? Great! Check out our migration guide for help
moving on to Enzyme v3 where React 16 is supported.


To get started with enzyme, you can simply install it via npm. You will need to install enzyme
along with an Adapter corresponding to the version of react (or other UI Component library) you
are using. For instance, if you are using enzyme with React 16, you can run:

  1. ``` sh
  2. npm i --save-dev enzyme enzyme-adapter-react-16
  3. ```

Each adapter may have additional peer dependencies which you will need to install as well. For instance,
enzyme-adapter-react-16 has peer dependencies on react and react-dom.

At the moment, Enzyme has adapters that provide compatibility with React 16.x, React 15.x,
React 0.14.x and React 0.13.x.

The following adapters are officially provided by enzyme, and have the following compatibility with
React:

EnzymeReact
------
`enzyme-adapter-react-16``^16.4.0-0`
`enzyme-adapter-react-16.3``~16.3.0-0`
`enzyme-adapter-react-16.2``~16.2`
`enzyme-adapter-react-16.1`~16.0.0-0
`enzyme-adapter-react-15``^15.5.0`
`enzyme-adapter-react-15.4``15.0.0-0
`enzyme-adapter-react-14``^0.14.0`
`enzyme-adapter-react-13``^0.13.0`

Finally, you need to configure enzyme to use the adapter you want it to use. To do this, you can use
the top level configure(...) API.

  1. ``` js
  2. import Enzyme from 'enzyme';
  3. import Adapter from 'enzyme-adapter-react-16';

  4. Enzyme.configure({ adapter: new Adapter() });
  5. ```

3rd Party Adapters
=============

It is possible for the community to create additional (non-official) adapters that will make enzyme
work with other libraries. If you have made one and it's not included in the list below, feel free
to make a PR to this README and add a link to it! The known 3rd party adapters are:

AdapterForStatus
---------
[`enzyme-adapter-preact-pure`](https://github.com/preactjs/enzyme-adapter-preact-pure)[`preact`](https://github.com/developit/preact)(stable)
|[`enzyme-adapter-inferno`](https://github.com/bbc/enzyme-adapter-inferno)|[`inferno`](https://github.com/infernojs/inferno)|(work

Running Enzyme Tests
===========

Enzyme is unopinionated regarding which test runner or assertion library you use, and should be
compatible with all major test runners and assertion libraries out there. The documentation and
examples for enzyme use Mocha and Chai, but you
should be able to extrapolate to your framework of choice.

If you are interested in using enzyme with custom assertions and convenience functions for
testing your React components, you can consider using:

[chai-enzyme](https://github.com/producthunt/chai-enzyme) with Mocha/Chai.
[jasmine-enzyme](https://github.com/FormidableLabs/enzyme-matchers/tree/master/packages/jasmine-enzyme) with Jasmine.
[jest-enzyme](https://github.com/FormidableLabs/enzyme-matchers/tree/master/packages/jest-enzyme) with Jest.
[should-enzyme](https://github.com/rkotze/should-enzyme) for should.js.
[expect-enzyme](https://github.com/PsychoLlama/expect-enzyme) for expect.












Basic Usage
===========


  1. ``` js
  2. import React from 'react';
  3. import { expect } from 'chai';
  4. import { shallow } from 'enzyme';
  5. import sinon from 'sinon';

  6. import MyComponent from './MyComponent';
  7. import Foo from './Foo';

  8. describe('<MyComponent />', () => {
  9.   it('renders three <Foo /> components', () => {
  10.     const wrapper = shallow(<MyComponent />);
  11.     expect(wrapper.find(Foo)).to.have.lengthOf(3);
  12.   });

  13.   it('renders an `.icon-star`', () => {
  14.     const wrapper = shallow(<MyComponent />);
  15.     expect(wrapper.find('.icon-star')).to.have.lengthOf(1);
  16.   });

  17.   it('renders children when passed in', () => {
  18.     const wrapper = shallow((
  19.       <MyComponent>
  20.         <div className="unique" />
  21.       </MyComponent>
  22.     ));
  23.     expect(wrapper.contains(<div className="unique" />)).to.equal(true);
  24.   });

  25.   it('simulates click events', () => {
  26.     const onButtonClick = sinon.spy();
  27.     const wrapper = shallow(<Foo onButtonClick={onButtonClick} />);
  28.     wrapper.find('button').simulate('click');
  29.     expect(onButtonClick).to.have.property('callCount', 1);
  30.   });
  31. });
  32. ```

Read the full API Documentation




  1. ``` js
  2. import React from 'react';
  3. import sinon from 'sinon';
  4. import { expect } from 'chai';
  5. import { mount } from 'enzyme';

  6. import Foo from './Foo';

  7. describe('<Foo />', () => {
  8.   it('allows us to set props', () => {
  9.     const wrapper = mount(<Foo bar="baz" />);
  10.     expect(wrapper.props().bar).to.equal('baz');
  11.     wrapper.setProps({ bar: 'foo' });
  12.     expect(wrapper.props().bar).to.equal('foo');
  13.   });

  14.   it('simulates click events', () => {
  15.     const onButtonClick = sinon.spy();
  16.     const wrapper = mount((
  17.       <Foo onButtonClick={onButtonClick} />
  18.     ));
  19.     wrapper.find('button').simulate('click');
  20.     expect(onButtonClick).to.have.property('callCount', 1);
  21.   });

  22.   it('calls componentDidMount', () => {
  23.     sinon.spy(Foo.prototype, 'componentDidMount');
  24.     const wrapper = mount(<Foo />);
  25.     expect(Foo.prototype.componentDidMount).to.have.property('callCount', 1);
  26.     Foo.prototype.componentDidMount.restore();
  27.   });
  28. });
  29. ```

Read the full API Documentation



  1. ``` js
  2. import React from 'react';
  3. import { expect } from 'chai';
  4. import { render } from 'enzyme';

  5. import Foo from './Foo';

  6. describe('<Foo />', () => {
  7.   it('renders three `.foo-bar`s', () => {
  8.     const wrapper = render(<Foo />);
  9.     expect(wrapper.find('.foo-bar')).to.have.lengthOf(3);
  10.   });

  11.   it('renders the title', () => {
  12.     const wrapper = render(<Foo title="unique" />);
  13.     expect(wrapper.text()).to.contain('unique');
  14.   });
  15. });
  16. ```

Read the full API Documentation

React Hooks support


Enzyme supports react hooks with some limitations in [.shallow()](https://enzymejs.github.io/enzyme/docs/api/shallow.html) due to upstream issues in React's shallow renderer:

useEffect() and useLayoutEffect() don't get called in the React shallow renderer. Related issue

useCallback() doesn't memoize callback in React shallow renderer. Related issue

[ReactTestUtils.act()](https://reactjs.org/docs/test-utils.html#act) wrap


If you're using React 16.8+ and .mount(), Enzyme will wrap apis including [.simulate()](https://enzymejs.github.io/enzyme/docs/api/ReactWrapper/simulate.html), [.setProps()](https://enzymejs.github.io/enzyme/docs/api/ReactWrapper/setProps.html), [.setContext()](https://enzymejs.github.io/enzyme/docs/api/ReactWrapper/setContext.html), [.invoke()](https://enzymejs.github.io/enzyme/docs/api/ReactWrapper/invoke.html) with [ReactTestUtils.act()](https://reactjs.org/docs/test-utils.html#act) so you don't need to manually wrap it.

A common pattern to trigger handlers with .act() and assert is:

  1. ``` js
  2. const wrapper = mount(<SomeComponent />);
  3. act(() => wrapper.prop('handler')());
  4. wrapper.update();
  5. expect(/* ... */);
  6. ```

We cannot wrap the result of .prop() (or .props()) with .act() in Enzyme internally since it will break the equality of the returned value.
However, you could use .invoke() to simplify the code:

  1. ``` js
  2. const wrapper = mount(<SomeComponent />);
  3. wrapper.invoke('handler')();
  4. expect(/* ... */);
  5. ```

Future




Contributing



In the wild


Organizations and projects using enzyme can list themselves here.

License