Enzyme

3.11.0 · abandoned · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';

// Configure Enzyme with the React 16 adapter
Enzyme.configure({ adapter: new Adapter() });

// A simple React component to test
function MyComponent({ name }) {
  return (
    <div>
      <h1 className="greeting">Hello, {name}!</h1>
      <button onClick={() => alert(`Clicked ${name}`)}>Click Me</button>
    </div>
  );
}

describe('MyComponent', () => {
  it('renders a greeting with the correct name', () => {
    const wrapper = shallow(<MyComponent name="World" />);
    expect(wrapper.find('.greeting').text()).toEqual('Hello, World!');
  });

  it('simulates click events', () => {
    const mockCallBack = jest.fn();
    const wrapper = shallow(<MyComponent name="Test" onClick={mockCallBack} />);
    wrapper.find('button').simulate('click');
    // In a real scenario, mockCallBack would be passed as a prop
    // and this test would verify if the prop function was called.
    // Here we're just showing the simulate usage.
    expect(true).toBe(true); // Placeholder for actual assertion
  });
});

view raw JSON →