Skip to content

react-test-renderer.create does not work properly with forwardRef components #12693

Closed
@ajcrites

Description

@ajcrites

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
If you try to access the .root of a component tree of a component created with forwardRef, you will get an error

Unexpected object passed to ReactTestInstance constructor

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

A minimal example is simply a component created with forwardRef:

import React from "react";

export default React.forwardRef(() => <div>hello</div>);

Now if you try to use create from react-test-renderer and access .root, you will get

Unexpected object passed to ReactTestInstance constructor (tag: 14). This is probably a bug in React.

import React from "react";
import { create } from "react-test-renderer";

import Hello from "./Hello";

it("test forwardRef component", () => {
  const tree = create(<Hello />);
  expect(tree.root);
});

https://codesandbox.io/s/vm98x95wx5

What is the expected behavior?
You should be able to create components created with forwardRef with the test renderer.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.3 using jest (forwardRef did not exist previously).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions