Enzyme provided several rendering modes, most notably shallow(), which renders only one level deep, treating child components as opaque placeholders rather than fully rendering them, and mount(), which renders the full component tree into a real DOM. Enzyme's API encouraged querying and asserting against a component's internal structure and implementation, like directly finding a specific child component by name or inspecting internal state, which meant tests could pass or fail based on internal refactoring details that a real user would never actually notice or care about — this implementation-detail coupling is the main reason the React community has largely moved toward React Testing Library instead, which deliberately queries the rendered output the way a real user would.
1Understanding Component Testing with Enzyme
Enzyme provided several rendering modes, most notably shallow(), which renders only one level deep, treating child components as opaque placeholders rather than fully rendering them, and mount(), which renders the full component tree into a real DOM. Enzyme's API encouraged querying and asserting against a component's internal structure and implementation, like directly finding a specific child component by name or inspecting internal state, which meant tests could pass or fail based on internal refactoring details that a real user would never actually notice or care about — this implementation-detail coupling is the main reason the React community has largely moved toward React Testing Library instead, which deliberately queries the rendered output the way a real user would.
If you're maintaining an older codebase using Enzyme, be aware that shallow rendering doesn't render child components at all, so any assertion depending on a child component's actual rendered output requires mount() instead, not shallow().
import { shallow } from 'enzyme';
import Greeting from './Greeting';
test('renders a greeting', () => {
const wrapper = shallow(<Greeting name="Ana" />);
expect(wrapper.text()).toContain('Ana');
});2Practical Example
Here is a real-world application of Component Testing with Enzyme showing how it is used in production React code.
import { shallow } from 'enzyme';
test('finds internal state directly, an implementation-detail-coupled test', () => {
const wrapper = shallow(<Counter />);
expect(wrapper.state('count')).toBe(0);
});3Best Practices
Follow these guidelines when working with Component Testing with Enzyme:
1. Recognize Enzyme as a largely legacy tool at this point, favoring React Testing Library for any new test code in a modern React codebase
2. Understand that shallow() renders only one level, treating children as placeholders, while mount() renders the full DOM tree, when maintaining existing Enzyme-based tests
3. Avoid writing new tests that assert on internal component state or implementation details, whether in Enzyme or any other tool, favoring user-visible behavior instead
Tip: If you're maintaining an older codebase using Enzyme, be aware that shallow rendering doesn't render child components at all, so any assertion depending on a child component's actual rendered output requires mount() instead, not shallow().
import { shallow } from 'enzyme';
import Greeting from './Greeting';
test('renders a greeting', () => {
const wrapper = shallow(<Greeting name="Ana" />);
expect(wrapper.text()).toContain('Ana');
});