Search⌘ K

Unit Tests with Enzyme

Explore how to use Enzyme to write unit tests for React V15 class components. Learn to apply shallow, mount, and render methods appropriately to test component behavior and lifecycle, helping you ensure your components work correctly and remain maintainable.

Introduction to Enzyme

Enzyme is a testing utility by Airbnb to assert, manipulate, and traverse React components. It is used to conduct unit tests to complement snapshot tests in React. First we have to install it along with its extension, since it doesn’t come with create-react-app.

Installing Enzyme

Shell
npm install --save-dev enzyme react-addons-test-utils enzyme-adapter-react-16

Second, we include it in the test setup and we initialize its adapter.

Javascript (babel-node)
import React from 'react';
import ReactDOM from 'react-dom';
import renderer from 'react-test-renderer';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import App, { Search, Button, Table } from './App';
Enzyme.configure({ adapter: new Adapter() });

Now you can write your first unit test in the Table “describe”-block. You will use ...