Posted in

How to configure Enzyme with Create React App?

If you’re building React applications with Create React App (CRA), you already have a powerful, streamlined workflow for development. But when it comes to testing component logic—especially for dynamic UIs with state, props, and user interactions—you need a tool that lets you render components, simulate events, and inspect outputs without spinning up a full browser. That’s where Enzyme comes in. As a provider specializing in testing utilities for React ecosystems, I’ve worked with dozens of teams that struggle to integrate Enzyme with their CRA setup, mostly because CRA’s default test configuration can clash with third-party testing libraries. Over the years, I’ve refined a step-by-step process that works for all modern CRA versions, and I’m sharing it here so you can skip the trial and error. Enzyme

First, let’s clear up a common misconception: Enzyme isn’t the only testing library for React, but it’s one of the most flexible for teams that need granular control over component behavior. Unlike React Testing Library (which prioritizes user-centric testing), Enzyme gives you APIs like shallow (for isolated component rendering), mount (for full DOM rendering), and render (for static HTML output) that make it easy to test edge cases—like ensuring a child component passes props correctly, or that a click handler updates state as expected. For teams that rely on these low-level checks, integrating Enzyme with CRA is non-negotiable.

Step 1: Check Your CRA and Enzyme Versions

Before you install anything, confirm you’re using compatible versions. CRA v3 and above use React 16.8+, and Enzyme v3 is the minimum required for React 16 compatibility. For React 18 projects, you’ll need Enzyme v3.11 or newer, as older versions don’t support React 18’s concurrent rendering features. If you’re working on an older CRA project (pre-v3), you’ll need to eject your configuration first, but I’ll focus on the default zero-config CRA setup that works for 90% of teams today.

Step 2: Install Dependencies Correctly

A lot of teams mess up installation by skipping peer dependencies, which leads to weird runtime errors. To avoid that, install Enzyme along with its required adapter for your React version, plus Jest’s Enzyme integration (since CRA uses Jest under the hood):

npm install --save-dev enzyme @wojtekmaj/enzyme-adapter-react-17 enzyme-to-json

Wait—why @wojtekmaj/enzyme-adapter-react-17 instead of the official enzyme-adapter-react-17? The official adapter stopped being maintained after React 17, and the community-driven version is the only one that works reliably with CRA v5 and newer. If you’re on React 18, use @wojtekmaj/enzyme-adapter-react-18 instead. And enzyme-to-json? That’s a helper that lets you convert Enzyme’s output to a format Jest can snapshot, which is critical for keeping your tests readable and maintainable.

Step 3: Configure Enzyme for Jest (CRA’s Test Runner)

CRA automatically sets up Jest, but you need to tell Jest to use your Enzyme adapter so it knows how to render React components. The best place to do this is in a setupTests.js file in your src directory—this is a special file CRA runs before every test, so it’s the perfect spot for one-time configurations. Create the file if it doesn’t exist, and add this code:

import Enzyme from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { createSerializer } from 'enzyme-to-json';

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

// Add enzyme-to-json as a Jest serializer for clean snapshots
expect.addSnapshotSerializer(createSerializer({ mode: 'deep' }));

That’s it for the core configuration. No need to edit any other CRA files, no ejecting, no complicated Babel or Jest tweaks. This works out of the box with CRA’s default setup because CRA’s setupTests.js is automatically included in the test run process.

Step 4: Write Your First Enzyme Test

Now let’s test a real component to make sure everything works. Let’s say you have a simple Button component in src/components/Button.js:

import React from 'react';

const Button = ({ label, onClick }) => {
  return (
    <button className="custom-button" onClick={onClick}>
      {label}
    </button>
  );
};

export default Button;

To test it with Enzyme, create a Button.test.js file in the same directory:

import React from 'react';
import { shallow, mount } from 'enzyme';
import Button from './Button';

// Test 1: Shallow render (isolated, no child components)
test('renders button with correct label', () => {
  const wrapper = shallow(<Button label="Click Me" />);
  expect(wrapper.text()).toBe('Click Me');
  expect(wrapper.hasClass('custom-button')).toBe(true);
});

// Test 2: Simulate a click event
test('calls onClick when button is clicked', () => {
  // Create a mock function to track if onClick is called
  const mockOnClick = jest.fn();
  const wrapper = shallow(<Button label="Click Me" onClick={mockOnClick} />);
  
  // Simulate a click on the button
  wrapper.find('button').simulate('click');
  
  // Verify the mock function was called once
  expect(mockOnClick).toHaveBeenCalledTimes(1);
});

// Test 3: Full DOM render (for components that need child interactions)
test('renders full component with children', () => {
  // Use mount instead of shallow to render all child components
  const wrapper = mount(<Button label="Submit" />);
  expect(wrapper.getDOMNode().textContent).toBe('Submit');
});

Run npm test in your terminal, and all three tests should pass. If you get an error here, it’s almost always because you installed the wrong adapter or forgot to configure the setupTests.js file—double-check those two steps before digging into more complex fixes.

Step 5: Troubleshoot Common CRA + Enzyme Issues

Even with a correct setup, you might run into one of these common problems. Let’s walk through how to fix them quickly:

  1. “Enzyme Internal Error: Unable to find node on the passed type”: This usually happens if you’re using shallow render and trying to find a child component. Remember, shallow only renders the component itself, not its children. Either use mount for full DOM rendering, or update your test to target a node on the parent component.
  2. Snapshot test formatting is messy: If your Jest snapshots include extra React internals, make sure you added enzyme-to-json as a serializer in setupTests.js. Without that, Enzyme’s output won’t cleanly serialize for snapshots.
  3. React 18 hydration errors in mount tests: React 18’s new root API can cause issues with mount in older Enzyme adapters. If you’re on React 18, switch to @wojtekmaj/enzyme-adapter-react-18 and make sure your test code doesn’t rely on ReactDOM.render (use mount instead, which works with React 18’s root).

Why This Setup Works for CRA Teams

CRA is designed to reduce friction for new React developers, but its default testing setup is limited. Enzyme fills that gap by giving teams the tools to test not just what users see, but how components behave under the hood. Whether you’re testing state changes, prop passing, or event handlers, the configuration I outlined above is lightweight, doesn’t require ejecting, and works with all standard CRA workflows.

As a provider of testing solutions for React development, we work with teams large and small to tailor Enzyme setups to their specific CRA projects. Whether you’re troubleshooting a tricky test case, looking to optimize your test suite’s performance, or need help integrating Enzyme with other tools like Cypress or Storybook, we can provide the support you need to get up and running quickly.

Food Colorant If your team is looking to implement robust component testing with Enzyme for your Create React App projects, or if you need to refine your existing test workflow, get in touch with us to discuss your requirements. We can provide custom configuration guidance, testing best practices, and scalable solutions that fit your team’s size and tech stack.

References

  1. Enzyme Official Documentation, https://enzymejs.github.io/enzyme/
  2. Create React App Testing Guide, https://create-react-app.dev/docs/running-tests/
  3. enzyme-to-json Repository, https://github.com/adriantoine/enzyme-to-json

Xi’an Sentian Biotechnology Co., Ltd.
Xi’an Sentian Biotechnology Co., Ltd. is one of the most professional enzyme manufacturers and suppliers in China, specialized in providing high quality customized service and OEM&ODM private label service. Be free to wholesale bulk enzyme in stock here from our factory. Contact us for free sample.
Address: Jingfa Building, No. 132 Weiyang Road, Weiyang District, Xi’an City, Shaanxi Province
E-mail: sales3@sentianbio.com
WebSite: https://www.sentianherb.com/