Skip to content

react-test-renderer renders attributes with value of undefined #10573

@msmichellegar

Description

@msmichellegar

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

Bug

What is the current behavior?

react-test-renderer appears to render attributes in a way that is inconsistent with React in the browser. When rendering with React, attributes with a value that is not truthy are omitted from the render. In react-test-renderer, an attribute with a value of undefined still renders.

If the current behaviour is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/84v837e9/).

With a component like this:

class Link extends React.Component {
  render() {
    return (
      <a href="/" target={this.props.newTab && '_blank'}>link!</a>
    );
  }
}

When rendering<Link newTab="true" />:

In React: <a href="/" target="_blank">link!</a>
In react-test-renderer: <a href="/" target="_blank">link!</a>

When rendering <Link />:

In React: <a href="/">link!</a>
In react-test-renderer: <a href="/" target={undefined}>link!</a>

What is the expected behavior?

I would expect react-test-renderer to have the same behaviour as React in the browser. So in this case, the undefined attribute would not be rendered at all.

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

Using version 15.6.1 of react-test-renderer and React, Chrome on a MacBook. Testing with Jest snapshots in version 19.0.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions