You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AGENTS.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Project Overview
4
4
5
-
**Test Renderer for React** is a lightweight, pure JavaScript testing library for React 19. It replaces the deprecated `react-test-renderer`. Built with `react-reconciler`, it outputs to a lightweight, traversable object structure (`HostElement`) for snapshot testing and asserting component output without a browser environment (DOM) or native dependencies.
5
+
**Test Renderer for React** is a lightweight, pure JavaScript testing library for React 19. It replaces the deprecated `react-test-renderer`. Built with `react-reconciler`, it outputs to a lightweight, traversable object structure (`TestInstance`) for snapshot testing and asserting component output without a browser environment (DOM) or native dependencies.
6
6
7
7
### Key Features
8
8
@@ -16,8 +16,8 @@
16
16
-**`src/index.ts`**: The public entry point that exports `createRoot` from `renderer.ts`.
17
17
-**`src/renderer.ts`**: Contains the main implementation. Exports `createRoot` which initializes the custom React reconciler.
18
18
-**`src/reconciler.ts`**: Implements the `react-reconciler` host config, translating React updates into operations on the internal tree.
19
-
-**`src/host-element.ts`**: Defines `HostElement`, a wrapper around the internal fiber nodes with a DOM-like API (e.g., `children`, `props`, `parent`).
20
-
-**`src/render-to-json.ts`**: Handles the serialization of `HostElement` trees into JSON format for snapshots.
19
+
-**`src/test-instance.ts`**: Defines `TestInstance`, a wrapper around the internal fiber nodes with a DOM-like API (e.g., `children`, `props`, `parent`).
20
+
-**`src/render-to-json.ts`**: Handles the serialization of `TestInstance` trees into JSON format for snapshots.
Copy file name to clipboardExpand all lines: README.md
+26-6Lines changed: 26 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,26 @@ This library supports all modern React features including:
44
44
- Error boundaries
45
45
- Suspense boundaries
46
46
47
+
## Test Output Tree
48
+
49
+
Instead of producing a DOM tree or a native view hierarchy, the renderer builds an in-memory **Test Output Tree**:
50
+
51
+
- Composed of **`TestNode`s**, where each node is either:
52
+
- A **`TestInstance`** — represents a host element such as `div` or `View`
53
+
- A plain **`string`** — represents a text node
54
+
- The root is accessible via `root.container`, a `TestInstance` whose `type` is an empty string
55
+
-`TestInstance` nodes are traversable and queryable — see the [`TestInstance`](#testelement) API below
56
+
57
+
## JSON Output Tree
58
+
59
+
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:
60
+
61
+
- Composed of **`JsonNode`s**, where each node is either:
62
+
- A **`JsonElement`** — a plain object with `type`, `props`, and `children`
63
+
- A plain **`string`** — a text node
64
+
- Contains no live references, making it safe to serialize
65
+
- Ideal for snapshot testing
66
+
47
67
## API Reference
48
68
49
69
### `createRoot(options?)`
@@ -58,7 +78,7 @@ Creates a new test renderer root instance.
58
78
59
79
-`render(element: ReactElement)`: Renders a React element into the root. Must be called within `act()`.
60
80
-`unmount()`: Unmounts the root and cleans up. Must be called within `act()`.
61
-
-`container`: A `HostElement` wrapper that contains the rendered element(s). Use this to query and inspect the rendered tree.
81
+
-`container`: A `TestInstance` wrapper that contains the rendered element(s). Use this to query and inspect the rendered tree.
62
82
63
83
**Example:**
64
84
@@ -84,22 +104,22 @@ Configuration options for the test renderer. Many of these options correspond to
84
104
|`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. |
85
105
|`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`. |
86
106
87
-
### `HostElement`
107
+
### `TestInstance` {#test-instance}
88
108
89
109
A wrapper around rendered host elements with a DOM-like API for querying and inspecting the rendered tree.
90
110
91
111
**Properties:**
92
112
93
113
-`type: string`: The element type (e.g., `"View"`, `"div"`). Returns an empty string for the container element.
94
-
-`props: HostElementProps`: The element's props object.
114
+
-`props: Record<string, all>`: The element's props object.
95
115
-`children: HostNode[]`: Array of child nodes (elements and text strings). Hidden children are excluded.
96
-
-`parent: HostElement | null`: The parent element, or `null` if this is the root container.
116
+
-`parent: TestInstance | null`: The parent element, or `null` if this is the root container.
97
117
-`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.
98
118
99
119
**Methods:**
100
120
101
121
-`toJSON(): JsonElement | null`: Converts this element to a JSON representation suitable for snapshots. Returns `null` if the element is hidden.
102
-
-`queryAll(predicate: (element: HostElement) => boolean, options?: QueryOptions): HostElement[]`: Finds all descendant elements matching the predicate. See [Query Options](#query-options) below.
122
+
-`queryAll(predicate: (instance: TestInstance) => boolean, options?: QueryOptions): TestInstance[]`: Finds all descendant elements matching the predicate. See [Query Options](#query-options) below.
0 commit comments