What is enzyme?
Enzyme is a JavaScript testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output. It is designed to work with test runners like Jest or Mocha, and it provides a more intuitive and flexible API for interacting with React component trees.
What are enzyme's main functionalities?
Shallow Rendering
Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child components.
import { shallow } from 'enzyme';
import MyComponent from './MyComponent';
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.my-class').length).toBe(1);
Full DOM Rendering
Full DOM rendering is ideal for use cases where you have components that may interact with DOM APIs, or may require the full lifecycle in order to fully test the component (i.e., componentDidMount etc.).
import { mount } from 'enzyme';
import MyComponent from './MyComponent';
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.my-class').length).toBe(1);
Static Rendering
Static rendering is used to render the component to static HTML. It's useful for rendering components to strings, for instance, to send in emails, or for analyzing the markup structure.
import { render } from 'enzyme';
import MyComponent from './MyComponent';
const wrapper = render(<MyComponent />);
expect(wrapper.find('.my-class').length).toBe(1);
Other packages similar to enzyme
react-testing-library
React Testing Library is a very popular alternative to Enzyme that provides light utility functions on top of react-dom and react-dom/test-utils, in a way that encourages better testing practices. It focuses on testing components 'as the user would' rather than testing implementation details.
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works well with React and can be used as a test runner, assertion library, and mocking library. Jest's snapshot testing feature can be seen as an alternative to Enzyme's rendering capabilities.