npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

test-renderer

v0.14.0

Published

A lightweight, JavaScript-only replacement for the deprecated React Test Renderer.

Readme

Test Renderer for React

A lightweight, JS-only building block for creating Testing Library-style libraries.

This library is used by React Native Testing Library but is written generically to support different React variants and custom renderers.

This library also serves as a replacement for the deprecated React Test Renderer. It is built using React Reconciler to provide a custom renderer that operates on host elements by default, with proper escape hatches when needed. Most React Reconciler options are exposed for maximum flexibility.

Installation

yarn add -D test-renderer

Getting Started

import { createRoot } from "test-renderer";
import { act } from "react";

test("renders a component", async () => {
  const renderer = createRoot();

  // Use `act` in async mode to allow resolving all scheduled React updates
  await act(async () => {
    renderer.render(<div>Hello!</div>);
  });

  expect(renderer.container).toMatchInlineSnapshot(`
    <>
      <div>
        Hello!
      </div>
    </>
  `);
});

API Reference

createRoot(options?)

Creates a new test renderer root instance.

Parameters:

  • options (optional): Configuration options for the renderer. See RootOptions below.

Returns: A Root object with the following properties:

  • render(element: ReactElement): Renders a React element into the root. Must be called within act().
  • unmount(): Unmounts the root and cleans up. Must be called within act().
  • container: A HostElement wrapper that contains the rendered element(s). Use this to query and inspect the rendered tree.

Example:

const renderer = createRoot();
await act(async () => {
  renderer.render(<div>Hello!</div>);
});

RootOptions

Configuration options for the test renderer. Many of these options correspond to React Reconciler configuration options. For detailed information about reconciler-specific options, refer to the React Reconciler source code.

| Option | Type | Description | | -------------------------- | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | textComponentTypes | string[] | Types of host components that are allowed to contain text nodes. Trying to render text outside of these components will throw an error. Useful for simulating React Native's text rendering rules. | | publicTextComponentTypes | string[] | Host component types to display to users in error messages when they try to render text outside of textComponentTypes. Defaults to textComponentTypes if not provided. | | createNodeMock | (element: ReactElement) => object | Function to create mock objects for refs. Called once per element that has a ref. Defaults to returning an empty object. | | identifierPrefix | string | A string prefix React uses for IDs generated by useId(). Useful to avoid conflicts when using multiple roots. | | isStrictMode | boolean | Enable React Strict Mode. When enabled, components render twice and effects run twice in development. | | onCaughtError | (error: unknown, errorInfo: { componentStack?: string }) => void | Callback called when React catches an error in an Error Boundary. Called with the error caught by the Error Boundary and an errorInfo object containing the component stack. | | onUncaughtError | (error: unknown, errorInfo: { componentStack?: string }) => void | Callback called when an error is thrown and not caught by an Error Boundary. Called with the error that was thrown and an errorInfo object containing the component stack. | | onRecoverableError | (error: unknown, errorInfo: { componentStack?: string }) => void | Callback called when React automatically recovers from errors. Called with an error React throws and an errorInfo object containing the component stack. Some recoverable errors may include the original error cause as error.cause. |

HostElement

A wrapper around rendered host elements that provides a DOM-like API for querying and inspecting the rendered tree.

Properties:

  • type: string: The element type (e.g., "View", "div"). Returns an empty string for the container element.
  • props: HostElementProps: The element's props object.
  • children: HostNode[]: Array of child nodes (elements and text strings). Hidden children are excluded.
  • parent: HostElement | null: The parent element, or null if this is the root container.
  • unstable_fiber: Fiber | null: Access to the underlying React Fiber node. Warning: This is an unstable API that exposes internal React Reconciler structures which may change without warning in future React versions. Use with caution and only when absolutely necessary.

Methods:

  • toJSON(): JsonElement | null: Converts this element to a JSON representation suitable for snapshots. Returns null if the element is hidden.
  • queryAll(predicate: (element: HostElement) => boolean, options?: QueryOptions): HostElement[]: Finds all descendant elements matching the predicate. See Query Options below.

Example:

const renderer = createRoot();
await act(async () => {
  renderer.render(<div className="container">Hello</div>);
});

const root = renderer.container.children[0] as HostElement;
expect(root.type).toBe("div");
expect(root.props.className).toBe("container");
expect(root.children).toContain("Hello");

QueryOptions

Options for configuring element queries.

| Option | Type | Default | Description | | ------------------ | --------- | ------- | -------------------------------------------------------------------------------------------------------------------- | | includeSelf | boolean | false | Include the element itself in the results if it matches the predicate. | | matchDeepestOnly | boolean | false | Exclude any ancestors of deepest matched elements even if they match the predicate. Only return the deepest matches. |

Example:

// Find all divs, including nested ones
const allDivs = container.queryAll((el) => el.type === "div");

// Find only the deepest divs (exclude parent divs if they contain matching children)
const deepestDivs = container.queryAll((el) => el.type === "div", { matchDeepestOnly: true });

// Include the container itself if it matches
const includingSelf = container.queryAll((el) => el.type === "div", { includeSelf: true });

Migration from React Test Renderer

This library serves as a replacement for the deprecated React Test Renderer. The main differences are:

  • Host element focus: This library operates on host components by default, while React Test Renderer worked with a mix of host and composite components. You can access the underlying fiber via unstable_fiber if needed.
  • Built on React Reconciler: This library is built using React Reconciler, providing a custom renderer implementation.
  • Exposed reconciler options: Most React Reconciler configuration options are exposed through RootOptions for maximum flexibility.

For most use cases, the migration is straightforward:

// Before (React Test Renderer)
import TestRenderer from "react-test-renderer";
const tree = TestRenderer.create(<MyComponent />);

// After (test-renderer)
import { createRoot } from "test-renderer";
const root = createRoot();
await act(async () => {
  root.render(<MyComponent />);
});
const tree = root.container;

Supported React Features

This library supports all modern React features including:

  • Concurrent rendering
  • Error boundaries
  • Suspense boundaries

License

MIT