react-test-renderer vs enzyme vs react-testing-library
React Testing Libraries
react-test-rendererenzymereact-testing-librarySimilar Packages:
React Testing Libraries

React Testing Libraries are tools designed to facilitate testing React components and applications. They provide APIs and utilities to simulate user interactions, validate component behavior, and ensure that the UI renders correctly. These libraries help developers write automated tests that can catch bugs early, improve code quality, and ensure that components behave as expected across different scenarios. They focus on testing components in a way that resembles how users interact with them, promoting best practices and encouraging tests that are resilient to implementation changes.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-test-renderer2,389,924242,036988 kB1,10525 days agoMIT
enzyme605,42219,896-2816 years agoMIT
react-testing-library44,916---7 years ago-
Feature Comparison: react-test-renderer vs enzyme vs react-testing-library

Rendering Methods

  • react-test-renderer:

    react-test-renderer only provides full rendering of components, creating a tree of React elements. It does not manipulate the DOM but instead renders components to a JSON-like structure, which can be used for snapshot testing and comparing the rendered output over time.

  • enzyme:

    enzyme provides three types of rendering: shallow, mount, and render. Shallow rendering allows you to render a component one level deep, which is useful for isolating a component and testing it without rendering its children. Mount rendering renders the component and all its children, providing a full DOM tree for more comprehensive testing. Render rendering creates a static HTML representation of the component, which is useful for snapshot testing.

  • react-testing-library:

    react-testing-library focuses on rendering components in a way that simulates real user interactions. It renders components into the DOM and provides utilities for querying and interacting with them, encouraging tests that reflect how users actually use the application.

Snapshot Testing

  • react-test-renderer:

    react-test-renderer is designed specifically for snapshot testing. It creates a tree of React elements that can be easily serialized and compared to previous snapshots, making it ideal for detecting visual regressions in components.

  • enzyme:

    enzyme supports snapshot testing through integration with Jest, but it is not its primary focus. You can create snapshots of rendered components, but enzyme is more geared towards interactive testing and inspecting component behavior.

  • react-testing-library:

    react-testing-library does not have built-in snapshot testing capabilities, but it can be used alongside Jest to create snapshots of rendered components. However, its primary focus is on testing functionality and user interactions rather than creating snapshots.

User Interaction Testing

  • react-test-renderer:

    react-test-renderer does not provide utilities for simulating user interactions. It focuses on rendering components and capturing their output for snapshot testing, but it does not handle event simulation or state manipulation.

  • enzyme:

    enzyme allows for simulating user interactions such as clicks, form submissions, and input changes. It provides methods for triggering events and manipulating component state, making it versatile for testing both UI behavior and internal logic.

  • react-testing-library:

    react-testing-library excels at testing user interactions by providing APIs for simulating events and interacting with components as a user would. It encourages testing components in a way that reflects real-world usage, making it more effective for validating UI behavior.

Focus on Implementation vs. Behavior

  • react-test-renderer:

    react-test-renderer focuses on the rendered output of components rather than their internal implementation. It encourages testing how components render and behave visually, which helps create more resilient tests that are less affected by changes in implementation.

  • enzyme:

    enzyme allows for testing both implementation details and component behavior. It provides access to component instances, state, and props, enabling detailed testing of how components work internally. However, this can lead to tests that are tightly coupled to the implementation, making them more fragile.

  • react-testing-library:

    react-testing-library emphasizes testing components from the user's perspective, focusing on their behavior and how they interact with the UI. It discourages testing implementation details, which leads to more robust and maintainable tests.

Code Example

  • react-test-renderer:

    Snapshot Testing with react-test-renderer

    import renderer from 'react-test-renderer';
    import MyComponent from './MyComponent';
    
    const tree = renderer.create(<MyComponent />).toJSON();
    
    // Create a snapshot
    expect(tree).toMatchSnapshot();
    
  • enzyme:

    Testing a Component with enzyme

    import { shallow } from 'enzyme';
    import MyComponent from './MyComponent';
    
    const wrapper = shallow(<MyComponent />);
    
    // Simulate a click event
    wrapper.find('button').simulate('click');
    
    // Check if the state has changed
    expect(wrapper.state('count')).toBe(1);
    
    // Check if the component rendered correctly
    expect(wrapper).toMatchSnapshot();
    
  • react-testing-library:

    User Interaction Testing with react-testing-library

    import { render, fireEvent } from '@testing-library/react';
    import MyComponent from './MyComponent';
    
    const { getByText } = render(<MyComponent />);
    
    // Simulate a user clicking a button
    fireEvent.click(getByText('Click Me'));
    
    // Check if the text has changed
    expect(getByText('Clicked!')).toBeInTheDocument();
    
How to Choose: react-test-renderer vs enzyme vs react-testing-library
  • react-test-renderer:

    Select react-test-renderer if you want to create snapshots of your components for visual regression testing. It is lightweight and integrates well with Jest for snapshot testing.

  • enzyme:

    Choose enzyme if you need a powerful and flexible testing utility that allows for shallow, deep, and full DOM rendering of components. It is particularly useful for testing component internals and state management.

  • react-testing-library:

    Opt for react-testing-library if you prioritize testing components from the user's perspective. It encourages best practices by focusing on how components are used rather than their internal implementation.

README for react-test-renderer

react-test-renderer (DEPRECATED)

Deprecation notice

react-test-renderer is deprecated and no longer maintained. It will be removed in a future version. As of React 19, you will see a console warning when invoking ReactTestRenderer.create().

React Testing

This library creates a contrived environment and its APIs encourage introspection on React's internals, which may change without notice causing broken tests. It is instead recommended to use browser-based environments such as jsdom and standard DOM APIs for your assertions.

The React team recommends @testing-library/react as a modern alternative that uses standard APIs, avoids internals, and promotes best practices.

React Native Testing

The React team recommends @testing-library/react-native as a replacement for react-test-renderer for native integration tests. This React Native testing-library variant follows the same API design as described above and promotes better testing patterns.

Documentation

This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment.

Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom.

Documentation: https://reactjs.org/docs/test-renderer.html

Usage:

const ReactTestRenderer = require('react-test-renderer');

const renderer = ReactTestRenderer.create(
  <Link page="https://www.facebook.com/">Facebook</Link>
);

console.log(renderer.toJSON());
// { type: 'a',
//   props: { href: 'https://www.facebook.com/' },
//   children: [ 'Facebook' ] }

You can also use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: https://jestjs.io/blog/2016/07/27/jest-14.html.