Skip to content

React-test-renderer: support for portal #11565

@alansouzati

Description

@alansouzati

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

Report a bug

What is the current behavior?

This test

import React from 'react';
import { createPortal } from 'react-dom';
import renderer from 'react-test-renderer';

const Drop = () => (
  createPortal(
    <div>hello</div>,
    this.dropContainer
  )
);

test('Drop renders', () => {
  const component = renderer.create(
    <div>
      <input />
      <Drop />
    </div>
  );
  const tree = component.toJSON();
  expect(tree).toMatchSnapshot();
});

fails with

Invariant Violation: Drop(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.

This test passes if I wrap createPortal in a container.

<div>
  {createPortal(
    <div>hello</div>,
    this.dropContainer
  )}
</div>

What is the expected behavior?

The code without the parent container works fine in the browser. So it seems that I'm adding the parent div just for the test to pass. I believe react-test-renderer should support empty returns?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

Lastest

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions