Enzyme React 16 Adapter

1.15.8 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure Enzyme with `enzyme-adapter-react-16` and then write a basic shallow rendering test for a React 16 functional component, asserting on its rendered output and prop updates. It assumes a test runner like Jest is set up.

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!');
  });
});

view raw JSON →