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

  1. Capture const root = renderer.root before calling unmount() and use that reference afterwards
  2. Move assertions before unmount(); create a fresh TestRenderer.create(element) for post-unmount checks
  3. 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

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


AI-assisted analysis of facebook/react@eafeac097b (2026-08-21). Data as JSON: /api/errors/f3c67e19dfcdcea4. Report an issue: GitHub.