W3cubDocs

/Enzyme

Installation

enzyme should be installed using npm:

npm i --save-dev enzyme

enzyme can be used with your test runner of choice. All examples in the documentation will be provided using mocha and BDD style chai, although neither library is a dependency of enzyme.

Working with React 16

If you are wanting to use enzyme with React 16, but don't already have React 16 and react-dom installed, you should do so:

npm i --save react@16 react-dom@16

Next, to get started with enzyme, you can simply install it with npm:

npm i --save-dev enzyme enzyme-adapter-react-16

And then you're ready to go! In your test files you can simply require or import enzyme:

ES6:

// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });
// test file
import { shallow, mount, render } from 'enzyme';

const wrapper = shallow(<Foo />);

ES5:

// setup file
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-16');

enzyme.configure({ adapter: new Adapter() });
// test file
var enzyme = require('enzyme');

var wrapper = enzyme.shallow(<Foo />);

Working with React 15

If you are wanting to use Enzyme with React 15, but don't already have React 15 and react-dom installed, you should do so:

npm i --save react@15 react-dom@15

Further, enzyme requires the test utilities addon be installed:

npm i --save-dev react-test-renderer@15

Next, to get started with enzyme, you can simply install it with npm:

npm i --save-dev enzyme enzyme-adapter-react-15

And then you're ready to go! In your test files you can simply require or import enzyme:

ES6:

// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-15';

configure({ adapter: new Adapter() });
// test file
import { shallow, mount, render } from 'enzyme';

const wrapper = shallow(<Foo />);

ES5:

// setup file
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-15');

enzyme.configure({ adapter: new Adapter() });
// test file
var enzyme = require('enzyme');

var wrapper = enzyme.shallow(<Foo />);

Working with React 0.14

If you are wanting to use Enzyme with React 0.14, but don't already have React 0.14 and react-dom installed, you should do so:

npm i --save [email protected] [email protected]

Further, enzyme with React 0.14 requires the test utilities addon be installed:

npm i --save-dev [email protected]

Next, to get started with enzyme, you can simply install it with npm:

npm i --save-dev enzyme enzyme-adapter-react-14

And then you're ready to go! In your test files you can simply require or import enzyme:

ES6:

// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-14';

configure({ adapter: new Adapter() });
// test file
import { shallow, mount, render } from 'enzyme';

const wrapper = shallow(<Foo />);

ES5:

// setup file
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-14');

enzyme.configure({ adapter: new Adapter() });
// test file
var enzyme = require('enzyme');

var wrapper = enzyme.shallow(<Foo />);

Working with React 0.13

If you are wanting to use enzyme with React 0.13, but don't already have React 0.13 installed, you should do so:

npm i [email protected] --save

Next, to get started with enzyme, you can simply install it with npm:

npm i --save-dev enzyme enzyme-adapter-react-13

And then you're ready to go! In your test files you can simply require or import enzyme:

ES6:

// setup file
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-13';

configure({ adapter: new Adapter() });
// test file
import { shallow, mount, render } from 'enzyme';

const wrapper = shallow(<Foo />);

ES5:

// setup file
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-13');

enzyme.configure({ adapter: new Adapter() });
// test file
var enzyme = require('enzyme');

var wrapper = enzyme.shallow(<Foo />);

© 2015 Airbnb, Inc.
Licensed under the MIT License.
https://airbnb.io/enzyme/docs/installation/