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-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,8 @@
13
13
14
14
### Architecture
15
15
16
-
-**`src/renderer.ts`**: The main entry point. Exports `createRoot` which initializes the custom React reconciler.
16
+
-**`src/index.ts`**: The public entry point that exports `createRoot` from `renderer.ts`.
17
+
-**`src/renderer.ts`**: Contains the main implementation. Exports `createRoot` which initializes the custom React reconciler.
17
18
-**`src/reconciler.ts`**: Implements the `react-reconciler` host config, translating React updates into operations on the internal tree.
18
19
-**`src/host-element.ts`**: Defines `HostElement`, a wrapper around the internal fiber nodes that provides a user-friendly, DOM-like API (e.g., `children`, `props`, `parent`).
19
20
-**`src/render-to-json.ts`**: Handles the serialization of `HostElement` trees into JSON format for snapshots.
@@ -41,7 +42,7 @@ The project uses **Bun** as the preferred runtime/package manager for developmen
41
42
## Development Conventions
42
43
43
44
-**Language:** Strict TypeScript.
44
-
-**Styling:** Code formatting is enforced by Prettier (`.prettierrc` implied by scripts).
45
+
-**Styling:** Code formatting is enforced by Prettier (configured via npm scripts, no explicit config file, uses defaults).
45
46
-**Testing:** Unit tests are located in `src/__tests__/`. Tests use `jest` and `ts-jest`.
46
47
-**Linting:** ESLint is used for static analysis.
47
48
-**Git:** Commits seem to follow standard conventions (implied by `release-it`).
A lightweight, JavaScript-only replacement for the deprecated React Test Renderer.
3
+
A lightweight, JS-only building block for creating Testing Library-style libraries.
4
4
5
-
## Why Use It?
5
+
This library is used by [React Native Testing Library](https://github.com/callstack/react-native-testing-library) but is written generically to support different React variants and custom renderers.
6
6
7
-
-**Pure JavaScript Testing** - Test React components in Jest or Vitest without browser or native dependencies
8
-
-**Universal** - Can be used to simulate React Native or any other React renderer
9
-
-**React 19 Ready** - Modern alternative as React Test Renderer is now deprecated
10
-
-**Lightweight** - Minimal dependencies and small bundle size
11
-
-**Type-safe** - Written in TypeScript with full type definitions
12
-
-**Flexible Configuration** - Customizable reconciler options for different use cases
7
+
This library also serves as a replacement for the deprecated React Test Renderer. It is built using [React Reconciler](https://github.com/facebook/react/tree/main/packages/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.
|`onCaughtError`|`(error, errorInfo) => void`| Called when Error Boundary catches an error |
69
-
|`onUncaughtError`|`(error, errorInfo) => void`| Called for uncaught errors |
70
-
|`onRecoverableError`|`(error, errorInfo) => void`| Called when React recovers from errors |
66
+
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](https://github.com/facebook/react/tree/main/packages/react-reconciler).
|`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. |
71
+
|`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. |
72
+
|`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. |
73
+
|`identifierPrefix`|`string`| A string prefix React uses for IDs generated by `useId()`. Useful to avoid conflicts when using multiple roots. |
74
+
|`isStrictMode`|`boolean`| Enable React Strict Mode. When enabled, components render twice and effects run twice in development. |
75
+
|`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. |
76
+
|`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. |
77
+
|`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`. |
71
78
72
79
### `HostElement`
73
80
74
-
The rendered element wrapper with a DOM-like API:
81
+
A wrapper around rendered host elements that provides a DOM-like API for querying and inspecting the rendered tree.
|`children`| Array of child elements and text strings |
81
-
|`parent`| Parent element or `null`|
82
-
|`toJSON()`| Convert to JSON for snapshots |
83
-
|`queryAll(predicate, options?)`| Find all matching descendant elements |
85
+
-`type: string`: The element type (e.g., `"View"`, `"div"`). Returns an empty string for the container element.
86
+
-`props: HostElementProps`: The element's props object.
87
+
-`children: HostNode[]`: Array of child nodes (elements and text strings). Hidden children are excluded.
88
+
-`parent: HostElement | null`: The parent element, or `null` if this is the root container.
89
+
-`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.
84
90
85
-
## Querying Elements
91
+
**Methods:**
86
92
87
-
Use `queryAll()` to find elements in the rendered tree:
93
+
-`toJSON(): JsonElement | null`: Converts this element to a JSON representation suitable for snapshots. Returns `null` if the element is hidden.
94
+
-`queryAll(predicate: (element: HostElement) => boolean, options?: QueryOptions): HostElement[]`: Finds all descendant elements matching the predicate. See [Query Options](#query-options) below.
|`includeSelf`|`boolean`|`false`| Include the element itself in the results if it matches the predicate. |
117
+
|`matchDeepestOnly`|`boolean`|`false`| Exclude any ancestors of deepest matched elements even if they match the predicate. Only return the deepest matches. |
119
118
120
-
Use `textComponents` to simulate React Native's text rendering rules:
This library serves as a replacement for the deprecated React Test Renderer. The main differences are:
135
+
136
+
-**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.
137
+
-**Built on React Reconciler**: This library is built using React Reconciler, providing a custom renderer implementation.
138
+
-**Exposed reconciler options**: Most React Reconciler configuration options are exposed through `RootOptions` for maximum flexibility.
141
139
142
-
Use `createNodeMock` to provide mock objects for refs:
140
+
For most use cases, the migration is straightforward:
0 commit comments