Unit Tests with Enzyme
In this lesson, we'll discuss unit testing in React using a testing utility called Enzyme.
We'll cover the following...
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
Press + to interact
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.
Press + to interact
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 ...
Access this course and 1400+ top-rated courses and projects.