mdjastrzebski/test-renderer
소개
A lightweight test renderer for React and a modern replacement for the deprecated React Test Renderer.
웹 및 앱측정된 스타 증가
- in 24h
- 0
- in 7 days
- +1
- in 15 days
- 기록 수집 중
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalTest Renderer for React
A lightweight test renderer for React and a modern replacement for the deprecated React Test Renderer.
This library is used by React Native Testing Library but should work with any React variant.
It uses React Reconciler to build a custom renderer that operates on host elements by default, and provides escape hatches for complex use-cases. Most React Reconciler options are exposed through RootOptions.
For release and compatibility policy, see docs/versioning.md.
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>
</>
`);
});
Documentation
- API Reference: full
RootOptions,TestInstance, andQueryOptions - Testing Async Code with
act: resolving promises, fake timers, and avoiding leakedactscopes - Migrating from React Test Renderer
- Performance Metrics
- Versioning & React 19 Compatibility
Test Output Tree
Instead of producing a DOM tree or a native view hierarchy, the renderer builds an in-memory Test Output Tree:
- Composed of
TestNodes, where each node is either:- A
TestInstance: represents a host element such asdivorView - A plain
string: represents a text node
- A
- The root is accessible via
root.container, aTestInstancewhosetypeis an empty string TestInstancenodes are traversable and queryable; see theTestInstanceAPI
JSON Output Tree
Calling toJSON() on a TestInstance produces a JSON Output Tree, a static, plain-object snapshot of the Test Output Tree at that point in time:
- Composed of
JsonNodes, where each node is either:- A
JsonElement: a plain object withtype,props, andchildren - A plain
string: a text node
- A
- Contains no live references, making it safe to serialize
- Ideal for snapshot testing
API Reference
createRoot(options?)
Creates a new test renderer root instance.
Parameters:
options(optional): Configuration options for the renderer. SeeRootOptionsbelow.
Returns: A Root object with the following properties:
render(element: ReactElement): Renders a React element into the root. Fragments are supported. Non-element root values such as strings ornullare not supported. Must be called withinact().unmount(): Unmounts the root and cleans up. Must be called withinact().container: ATestInstancewrapper 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
Optional configuration passed to createRoot. Common options:
textComponentTypes/publicTextComponentTypes: Host component types allowed to contain text nodes (useful for simulating React Native's text rendering rules).isStrictMode: Enable React Strict Mode.identifierPrefix: Prefix for IDs generated byuseId().onCaughtError/onUncaughtError/onRecoverableError: Error-handling callbacks.
The full options table, along with the TestInstance and QueryOptions APIs, is in the API Reference.
License
MIT
관련 프로젝트 살펴보기
불러오는 중…