Deprecated React renderer for creating pure JavaScript object representations of React component trees for testing purposes
npx @tessl/cli install tessl/npm-react-test-renderer@19.1.0React Test Renderer is a deprecated React renderer that provides an experimental API for creating pure JavaScript object representations of React component trees without requiring a DOM or native mobile environment. It enables snapshot testing by rendering React components to JavaScript objects that can be serialized and compared across test runs.
⚠️ DEPRECATION NOTICE: This package is deprecated as of React 19 and will be removed in a future version. The React team recommends @testing-library/react for DOM-based testing and @testing-library/react-native for React Native testing.
npm install react-test-rendererimport { create, act } from 'react-test-renderer';For CommonJS:
const { create, act } = require('react-test-renderer');import React from 'react';
import { create } from 'react-test-renderer';
// Create a test renderer instance
const component = create(<div>Hello World</div>);
// Get JSON representation for snapshot testing
const json = component.toJSON();
console.log(json);
// { type: 'div', props: {}, children: ['Hello World'] }
// Access the root instance for traversal
const root = component.root;
const divElement = root.findByType('div');
// Update with new element
component.update(<div>Updated Content</div>);
// Clean up
component.unmount();React Test Renderer is built around several key components:
create() function that instantiates a test renderer for a React elementCore functionality for creating test renderer instances and managing their lifecycle. Essential for all testing scenarios.
function create(
element: React.ReactElement<any>,
options?: TestRendererOptions
): TestRenderer;
interface TestRendererOptions {
createNodeMock?: (element: React.ReactElement<any>) => any;
unstable_isConcurrent?: boolean;
unstable_strictMode?: boolean;
}
interface TestRenderer {
toJSON(): ReactTestRendererNode | Array<ReactTestRendererNode> | null;
toTree(): any;
update(element: React.ReactElement<any>): void;
unmount(): void;
getInstance(): React.Component<any, any> | any | null;
readonly root: ReactTestInstance;
unstable_flushSync: (fn: () => void) => void;
}ReactTestInstance methods for navigating and searching the rendered component tree. Critical for making assertions about rendered components.
class ReactTestInstance {
readonly type: any;
readonly props: object;
readonly parent: ReactTestInstance | null;
readonly children: Array<ReactTestInstance | string>;
readonly instance: any;
find(predicate: (node: ReactTestInstance) => boolean): ReactTestInstance;
findByType(type: any): ReactTestInstance;
findByProps(props: object): ReactTestInstance;
findAll(predicate: (node: ReactTestInstance) => boolean, options?: FindOptions): Array<ReactTestInstance>;
findAllByType(type: any, options?: FindOptions): Array<ReactTestInstance>;
findAllByProps(props: object, options?: FindOptions): Array<ReactTestInstance>;
}
interface FindOptions {
deep?: boolean;
}Testing utilities for managing React updates and batching.
function act(callback: () => void | Promise<void>): Promise<void>;
function unstable_batchedUpdates<T>(callback: () => T): T;type ReactTestRendererNode = ReactTestRendererJSON | string;
interface ReactTestRendererJSON {
type: string;
props: { [propName: string]: any };
children: Array<ReactTestRendererNode> | null;
$$typeof?: symbol;
}
type Predicate = (node: ReactTestInstance) => boolean | null;const version: string;
const _Scheduler: any;The shallow renderer (react-test-renderer/shallow) has been completely removed as of React 19. Attempting to import it will throw an error:
// This will throw an error
import shallow from 'react-test-renderer/shallow';
// Error: "react-test-renderer/shallow has been removed. See https://react.dev/warnings/react-test-renderer."