facebook/react · error · Error
Can't access .root on unmounted test renderer
Error message
Can't access .root on unmounted test renderer
What it means
TestRenderer.root is a getter that throws when the internal root is null, which happens after testRenderer.unmount() has destroyed the root. Once unmounted there is no fiber tree to wrap, so accessing .root is treated as an error rather than returning a stale instance.
Source
Thrown at packages/react-test-renderer/src/ReactTestRenderer.js:613
container = null;
root = null;
},
getInstance() {
if (root == null || root.current == null) {
return null;
}
return getPublicRootInstance(root);
},
unstable_flushSync: flushSyncFromReconciler,
};
Object.defineProperty(entry, 'root', {
configurable: true,
enumerable: true,
get: function () {
if (root === null) {
throw new Error("Can't access .root on unmounted test renderer");
}
const children = getChildren(root.current);
if (children.length === 0) {
throw new Error("Can't access .root on unmounted test renderer");
} else if (children.length === 1) {
// Normally, we skip the root and just give you the child.
return children[0];
} else {
// However, we give you the root if there's more than one root child.
// We could make this the behavior for all cases but it would be a breaking change.
// $FlowFixMe[incompatible-use] found when upgrading Flow
return wrapFiber(root.current);
}
},
} as Object);
return entry;
}View on GitHub (pinned to eafeac097b)
Solutions
- Capture const root = renderer.root before calling unmount() and use that reference afterwards
- Move assertions before unmount(); create a fresh TestRenderer.create(element) for post-unmount checks
- Keep unmount() in afterEach but stop touching .root after it runs
Example fix
// before const renderer = TestRenderer.create(<App />); renderer.unmount(); const root = renderer.root; // throws // after const renderer = TestRenderer.create(<App />); const root = renderer.root; // grab first renderer.unmount(); // root already captured for any needed assertions
Defensive patterns
Strategy: validation
Validate before calling
// Capture the root before unmount const renderer = TestRenderer.create(<App />); const root = renderer.root; // safe here renderer.unmount(); // subsequent code uses `root`, never `renderer.root`
Prevention
- Store renderer.root in a variable immediately after create() and use only that reference
- Keep unmount() last in the test; never access .root after cleanup
When it happens
Trigger: Calling testRenderer.unmount() and then reading testRenderer.root in a later assertion or afterEach hook.
Common situations: Cleanup code between tests that unmounts but keeps a module-level renderer reference; unmount-then-inspect test patterns that assert on post-unmount state via .root.
Related errors
- No instances found ${message}
- Expected 1 but found ${all.length} instances ${message}
- react-dom/unstable_testing is not supported in React Server
- 361
- Unable to find node on an unmounted component.
AI-assisted analysis of facebook/react@eafeac097b (2026-08-21).
Data as JSON: /api/errors/f3c67e19dfcdcea4.
Report an issue: GitHub.