What is react-test-renderer?
The react-test-renderer package is designed for testing React components in a more efficient and lightweight manner. It provides a renderer that can be used to render React components into pure JavaScript objects without depending on the DOM or a native mobile environment. This makes it ideal for testing components in isolation from the browser or any other external environment.
What are react-test-renderer's main functionalities?
Rendering components to JSON
This feature allows you to render a React component and generate a JSON output of the rendered component. This is useful for snapshot testing, where you can compare the current version of the component with a previously stored snapshot.
import React from 'react';
import renderer from 'react-test-renderer';
const component = renderer.create(<MyComponent />);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
Testing component output
This feature enables testing of the component's output. You can inspect the rendered component to verify that it has the expected props, state, or behavior. This is useful for ensuring that your component behaves correctly under various conditions.
import React from 'react';
import renderer from 'react-test-renderer';
const component = renderer.create(<MyComponent prop='value' />);
const instance = component.root;
expect(instance.props.prop).toBe('value');
Interacting with component instances
This feature allows you to interact with the component instances directly. You can simulate events or call methods on the component instance and then re-render. This is useful for testing the component's behavior in response to user interactions or lifecycle events.
import React from 'react';
import renderer from 'react-test-renderer';
class MyComponent extends React.Component {
state = { clicked: false };
handleClick = () => {
this.setState({ clicked: true });
};
render() {
return (<button onClick={this.handleClick}>Click me</button>);
}
}
const component = renderer.create(<MyComponent />);
const button = component.root.findByType('button');
button.props.onClick();
expect(component.toJSON()).toMatchSnapshot();
Other packages similar to react-test-renderer
enzyme
Enzyme is a popular testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output. Unlike react-test-renderer, Enzyme allows you to simulate events and provides a more jQuery-like API for selecting elements. However, it requires an adapter to work with different versions of React.
react-testing-library
React Testing Library is a set of helpers that let you test React components without relying on their implementation details. This approach encourages better testing practices by focusing on the behavior of components rather than their internal structure. It's more aligned with user interaction compared to react-test-renderer, which is more about rendering components to JSON for snapshot testing.
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());
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.
19.1.0 (March 28, 2025)
Owner Stack
An Owner Stack is a string representing the components that are directly responsible for rendering a particular component. You can log Owner Stacks when debugging or use Owner Stacks to enhance error overlays or other development tools. Owner Stacks are only available in development builds. Component Stacks in production are unchanged.
- An Owner Stack is a development-only stack trace that helps identify which components are responsible for rendering a particular component. An Owner Stack is distinct from a Component Stacks, which shows the hierarchy of components leading to an error.
- The captureOwnerStack API is only available in development mode and returns a Owner Stack, if available. The API can be used to enhance error overlays or log component relationships when debugging. #29923, #32353, #30306,
#32538, #32529, #32538
React
- Enhanced support for Suspense boundaries to be used anywhere, including the client, server, and during hydration. #32069, #32163, #32224, #32252
- Reduced unnecessary client rendering through improved hydration scheduling #31751
- Increased priority of client rendered Suspense boundaries #31776
- Fixed frozen fallback states by rendering unfinished Suspense boundaries on the client. #31620
- Reduced garbage collection pressure by improving Suspense boundary retries. #31667
- Fixed erroneous “Waiting for Paint” log when the passive effect phase was not delayed #31526
- Fixed a regression causing key warnings for flattened positional children in development mode. #32117
- Updated
useId
to use valid CSS selectors, changing format from :r123:
to «r123»
. #32001
- Added a dev-only warning for null/undefined created in useEffect, useInsertionEffect, and useLayoutEffect. #32355
- Fixed a bug where dev-only methods were exported in production builds. React.act is no longer available in production builds. #32200
- Improved consistency across prod and dev to improve compatibility with Google Closure Complier and bindings #31808
- Improve passive effect scheduling for consistent task yielding. #31785
- Fixed asserts in React Native when passChildrenWhenCloningPersistedNodes is enabled for OffscreenComponent rendering. #32528
- Fixed component name resolution for Portal #32640
- Added support for beforetoggle and toggle events on the dialog element. #32479 #32479
React DOM
- Fixed double warning when the
href
attribute is an empty string #31783
- Fixed an edge case where
getHoistableRoot()
didn’t work properly when the container was a Document #32321
- Removed support for using HTML comments (e.g.
<!-- -->
) as a DOM container. #32250
- Added support for
<script>
and <template>
tags to be nested within <select>
tags. #31837
- Fixed responsive images to be preloaded as HTML instead of headers #32445
use-sync-external-store
- Added
exports
field to package.json
for use-sync-external-store
to support various entrypoints. #25231
React Server Components
- Added
unstable_prerender
, a new experimental API for prerendering React Server Components on the server #31724
- Fixed an issue where streams would hang when receiving new chunks after a global error #31840, #31851
- Fixed an issue where pending chunks were counted twice. #31833
- Added support for streaming in edge environments #31852
- Added support for sending custom error names from a server so that they are available in the client for console replaying. #32116
- Updated the server component wire format to remove IDs for hints and console.log because they have no return value #31671
- Exposed
registerServerReference
in client builds to handle server references in different environments. #32534
- Added react-server-dom-parcel package which integrates Server Components with the Parcel bundler #31725, #32132, #31799, #32294, #31741