enzymejs / chai-enzyme

Chai.js assertions and convenience functions for testing React Components with enzyme
MIT License
787 stars 72 forks source link

Error message for shallow render contains #218

Closed armsteadj1 closed 6 years ago

armsteadj1 commented 6 years ago

Currently it shows you the entire render html when this errors.

For example:

function Bar() {
  return (
    <div>
      <div className="in-bar" />
    </div>
  );
}
function Foo() {
  return (
    <div>
      <Bar attr="12" />
    </div>
  );
}
expect(shallow(<Foo>)).to.contain(<Bar attr="13" />);

when this errors it does a .html() on the wrapper and would show:

Failure: Expected <Foo /> to contain <Bar attr="13" />

<div>
    <div>
      <div className="in-bar" />
    </div>
</div>

This makes it extremely hard to understand exactly why the test is failing.

Would anyone be against adding an overload to do a .debug() instead so the following would be output. Or better yet, always use debug for shallow wrappers.

So the following would be shown:

Failure: Expected <Foo /> to contain <Bar attr="13" />

<div>
    <Bar attr="12">
</div>

Thoughts?

ljharb commented 6 years ago

That seems way better to me.

ayrton commented 6 years ago

Sounds more than reasonable

armsteadj1 commented 6 years ago

Doing it for only Shallow is actually what jest-enzyme does as well:

https://github.com/FormidableLabs/enzyme-matchers/blob/master/packages/enzyme-matchers/src/utils/html.js

armsteadj1 commented 6 years ago

I'll get a pull request created. Thanks!