question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change in _owner causes issues with shallow rendered tests

See original GitHub issue

There appears to have been a change in how _owner is generated between 0.13 and 0.14 which is causing a number of our shallow rendered tests to fail when upgrading because the deepEquals fails on the _owner property. Here is a simple example component which exhibits the issue.

'use strict';

fdescribe('ComponentTest', function() {

  it('passes in 0.13 but fails in 0.14', function() {
    var components = {};
    components.tester = React.createClass({

      getInitialState: function() {
        return this.generateState();
      },

      generateState: function() {
        var state = {};
        state.body = (
          <ul className="foo">
            <li><div>Item 1</div></li>
            <li>Item 2</li>
          </ul>);
        return state;
      },

      render: function() {
        return (
          <div className="foo">
            {this.state.body}
          </div>);
      }
    });

    var shallowRenderer = testUtils.createRenderer();
    shallowRenderer.render(<components.tester />);
    var output = shallowRenderer.getRenderOutput();

    assert.deepEqual(output,
      <div className="foo">
        <ul className="foo">
          <li><div>Item 1</div></li>
          <li>Item 2</li>
        </ul>
      </div>);
  });
});
    AssertionError: expected { '$typeof': 60103,
      type: 'div',
      key: null,
      ref: null,
      props: 
       { className: 'foo',
         children: 
          { '$typeof': 60103,
            type: 'ul',
            key: null,
            ref: null,
            props: [Object],
            _owner: [Object],
            _store: {} } },
      _owner: null,
      _store: {} } to deeply equal { '$typeof': 60103,
      type: 'div',
      key: null,
      ref: null,
      props: 
       { className: 'foo',
         children: 
          { '$typeof': 60103,
            type: 'ul',
            key: null,
            ref: null,
            props: [Object],
            _owner: null,
            _store: {} } },
      _owner: null,
      _store: {} }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Apr 14, 2016

This should be fixed by #6362.

0reactions
vfilcommented, Apr 9, 2016

I prefer applying a monkey patch, until we have this fixed in react by wrapping TestUtils in my own module:

function render(component) {
    const renderer = TestUtils.createRenderer()
    renderer.render(component)
    return patch(renderer.getRenderOutput())
}

function fixOwner(obj) {
    return obj._owner ? Object.assign({}, obj, {_owner: null}) : Object.assign({}, obj)
}

function patch(obj) {
    if (Array.isArray(obj)) {
        return obj.map(comp => {
            return fixChildren(comp)
        })
    } else {
        obj = fixOwner(obj)
        if (obj.props && typeof obj.props.children === 'object') {
            obj.props = fixOwner(obj.props)
            obj.props.children = fixChildren(obj.props.children)
        }
        return obj
    }
}

https://gist.github.com/vfil/81049b1590aa3b9e439188a49a5d67bc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why I Always Use Shallow Rendering | HackerNoon
The first reason — it's slow. Not actually “just” slow, but “unpredictable” slow. mount renders just everything. There is no limits. When you ......
Read more >
In Defense of Shallow Rendering - JavaScript in Plain English
Shallow rendering lets you render a component “one level deep” and assert facts about what its render method returns, without worrying about the...
Read more >
The Dangers of Shallow Rendering - Mark Skelton
Three reasons why you should avoid shallow rendering when writing JavaScript component tests. Sign reading “danger no entry”.
Read more >
Why I Never Use Shallow Rendering - Kent C. Dodds
The reason this kind of test fails those considerations is because it's testing irrelevant implementation details. The user doesn't care one bit ...
Read more >
How to shallow test a non redux connected component that ...
I've had problems working with shallow rendering with enzyme. Currently, enzyme does not support react hooks fully in shallow rendered test ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found