Valerie Kraucunas
Please ask questions as they come to you.
If you have a better answer than I do, please contribute.
Started by Airbnb engineer Leland Richardson, since open sourced
Enzyme is a JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output.
Benefits of using Enzyme
Enzyme makes asking questions about the rendered output of your React components easy and intuitive by providing a fluent interface around rendered React components.
So uh... what am I supposed to be testing?
Getting Started
npm i --save-dev enzyme
npm i --save-dev react-addons-test-utils
npm i --save-dev react-domnpm i --save-dev chai-enzyme
npm i --save-dev jasmine-enzyme
npm i --save-dev jest-enzyme
npm i --save-dev should-enzymeOptional Assertion Libraries
*
Example of basic test
import React from 'react';
import { shallow } from 'enzyme';
import ToDoList from '../components/ToDoList';
import ToDoItem from '../components/ToDoItem';
describe('<ToDoList />', () => {
it('renders the entire list of items', () => {
const items = [mockItem(), mockItem() /*, ... */];
const wrapper = shallow(<ToDoList items={items} />);
expect(wrapper.find(ToDoList)).to.have.length(items.length);
});
});Example of complex test
import React from 'react';
import { shallow } from 'enzyme';
import chai, {expect} from 'chai';
import { stub } from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
const FBButton4 = ({ likePage }) => {
const onClick = () => {
if (FB.like()) {
likePage();
}
};
return <button onClick={onClick}>Like this on Fakebook!</button>;
};
describe('<FBButton4 />', () => {
it('will call our action because FB.like returns `true`', () => {
const likePage = stub();
const wrapper = shallow(<FBButton4 likePage={likePage} />);
stub(FB, 'like').returns(true);
wrapper.find('button').simulate('click');
expect(likePage).to.have.been.called;
FB.like.restore(); // This is important!
});
});Shallow
Mount
Render
Shallow
Purest unit testing option
No children rendered, to ensure that your tests aren't indirectly asserting on behavior of child components.
Fastest rendering method, works best on stateless components
Mount
Full DOM rendering, including children
If you do not want to run your tests inside of a browser, the recommended approach to using mount is to depend on a library called jsdom which is essentially a headless browser implemented completely in JS.
Includes the most lifecycle testing
Render
Renders static html, including children, fewest lifecycle methods covered
Uses Cheerio
Render is useful when you just want to assert on the DOM your component(s) render, lets you get around incorporating jsdom for mount
What about React Native?
npm i --save-dev react-native-mock
// test script in package.json
mocha --require react-native-mock/mock --recursive path/to/test/dirReact Native has many environmental dependencies that can be hard to simulate without a host device.
Test double library!
npm i --save-dev sinonAllows you to replace the difficult parts of your tests with something that makes testing simple.
Test double library!
Valerie Kraucunas
valeriekraucunas@gmail.com
vkraucunas