Enzyme is a JavaScript Testing utility for React, designed to simplify the testing of React Components' output by providing methods to manipulate, traverse, and simulate runtime. Its API was influenced by jQuery, offering an intuitive way to interact with component trees. The last stable version released was 3.11.0, published in December 2019. Enzyme required specific 'adapter' packages (e.g., `enzyme-adapter-react-16`) to link with particular React versions. However, Enzyme is no longer actively maintained and has not received official updates or support for React 17, 18, or subsequent versions, due to significant changes in React's internal APIs. While unofficial community-maintained adapters exist for newer React versions, they are not officially supported and may have limitations. The project is effectively abandoned, and the React community has largely shifted towards `React Testing Library` for component testing, which promotes testing from a user's perspective rather than implementation details.
npm install enzymeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure Enzyme with an adapter for React 16 and perform basic shallow rendering and event simulation on a functional React component.
Migrate testing suites to `React Testing Library` for modern React versions. If migration is not immediately feasible, consider using unofficial adapters (e.g., `@wojtekmaj/enzyme-adapter-react-17` or `@cfaester/enzyme-adapter-react-18`) with caution, as they are not guaranteed to be stable or fully compatible.
Adopt `React Testing Library` and shift testing methodology to focus on how users interact with your components rather than their internal structure.
Ensure `npm i --save-dev enzyme-adapter-react-XX` is run, and `Enzyme.configure({ adapter: new Adapter() })` is called in your test setup file, matching your React version.For projects utilizing Hooks, Concurrent Mode, or Suspense, migrating to `React Testing Library` is strongly recommended as Enzyme cannot reliably test these features.
Create a test setup file (e.g., `src/setupTests.js` for Create React App) and add:
```javascript
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16'; // Or the appropriate adapter for your React version
Enzyme.configure({ adapter: new Adapter() });
```
Ensure this setup file is correctly referenced by your test runner (e.g., `jest --config '{"setupFilesAfterEnv": ["<rootDir>/src/setupTests.js"]}''`).Verify that the selector used to find the element (`wrapper.find('button')`) is correct and that the component renders as expected. If using `shallow`, ensure the target element is directly rendered by the component being tested or use `.dive()` if interacting with a child component. Ensure you are not trying to simulate events on a non-interactive element or an element that doesn't exist.When testing connected components, either shallow render the unconnected component directly, or `mount` the connected component wrapped in a `Provider` with a mock store. For `shallow` tests, use `shallow(<ConnectedComponent store={mockStore} />)` or `shallow(<ConnectedComponent />).dive()` to get to the underlying component if it's connected.This warning indicates Enzyme's fundamental incompatibility with modern React rendering mechanisms. While unofficial adapters like `@cfaester/enzyme-adapter-react-18` attempt to bridge this, they are workarounds. The recommended fix is to migrate tests to `React Testing Library`.