Enzyme React 16 Adapter
This package, `enzyme-adapter-react-16`, provides the necessary compatibility layer for the Enzyme JavaScript testing utility to work with React 16.x components. Enzyme, currently at version 3.x for its core, offers an intuitive, jQuery-like API for manipulating and traversing React component outputs in tests, making it easier to assert on their structure and behavior. This adapter is crucial for projects relying on Enzyme to test React 16 applications, allowing developers to shallow render or mount components and simulate interactions. While `enzyme-adapter-react-16` (last updated to v1.15.8) effectively supports React 16, the broader Enzyme ecosystem has largely entered a maintenance or abandoned state for newer React versions (17+). For modern React development, React Testing Library is now the recommended and actively maintained alternative.
Common errors
-
Invariant Violation: Adapter not found.
cause Enzyme was imported but not configured with an adapter.fixYou must call `Enzyme.configure({ adapter: new Adapter() });` at the beginning of your test setup file (e.g., `setupTests.js` or `setupFilesAfterEnv` in Jest). -
Cannot read properties of undefined (reading 'configure')
cause The `Enzyme` object was not correctly imported or is undefined when attempting to call `configure`.fixEnsure `import Enzyme from 'enzyme';` is present and correctly resolved by your module system. Verify `enzyme` is installed as a dependency. -
TypeError: Cannot read properties of undefined (reading 'simulate')
cause This often occurs when trying to simulate events on a component rendered with `shallow` that itself contains a DOM element or component which is not directly rendered by the parent (e.g., trying to simulate click on a child component's internal button). `shallow` rendering only renders the component one level deep, not its children's internals. Or it may indicate the element you are trying to simulate on doesn't exist.fixFor simulating interactions that require the full component tree, use `mount` instead of `shallow`. Alternatively, ensure the element you are selecting exists within the shallowly rendered component's immediate output, or use `dive()` to interact with the internals of a direct child. Remember `mount` requires a DOM environment (like JSDOM).
Warnings
- breaking Enzyme v3 introduced breaking changes from v2, particularly around how adapters are configured and internal API changes due to React 16's updated reconciler. Projects upgrading from Enzyme 2.x or React versions prior to 16 must consult the migration guide.
- gotcha This adapter is strictly for React 16. Using it with other React versions (e.g., React 15, 17, 18) will lead to compatibility issues or errors. Each major React version typically requires its specific Enzyme adapter.
- deprecated The Enzyme library and its adapters are no longer actively maintained for recent React versions (React 17, 18+). It has fallen behind changes in React's internal implementation (e.g., Hooks, Suspense, Concurrent Mode), leading to incomplete support and a generally stalled development. The community has largely moved to React Testing Library as the de-facto standard for React component testing.
Install
-
npm install enzyme-adapter-react-16 -
yarn add enzyme-adapter-react-16 -
pnpm add enzyme-adapter-react-16
Imports
- Enzyme
const Enzyme = require('enzyme');import Enzyme from 'enzyme';
- Adapter
const Adapter = require('enzyme-adapter-react-16');import Adapter from 'enzyme-adapter-react-16';
- configure
configure({ adapter: new Adapter() });Enzyme.configure({ adapter: new Adapter() });
Quickstart
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import { shallow } from 'enzyme';
// Configure Enzyme with the React 16 adapter
Enzyme.configure({ adapter: new Adapter() });
// A simple React component for testing
const MyComponent = ({ name }) => <h1>Hello, {name}!</h1>;
describe('MyComponent', () => {
it('renders a greeting with the provided name', () => {
const wrapper = shallow(<MyComponent name="World" />);
expect(wrapper.find('h1').text()).toEqual('Hello, World!');
});
it('updates the greeting when props change', () => {
const wrapper = shallow(<MyComponent name="Initial" />);
expect(wrapper.find('h1').text()).toEqual('Hello, Initial!');
wrapper.setProps({ name: 'Updated' });
expect(wrapper.find('h1').text()).toEqual('Hello, Updated!');
});
});