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.

Assertion for shallow rendering `equals`

See original GitHub issue

It would be nice to have assertion to check node against node, equals from shallow rendering of enzyme (probably equalsJSX, same to https://github.com/algolia/expect-jsx)

Now

expect(shallow(<Component />).find('SubComponent').equals(<SubComponent foo="bar" />)).to.be.true

Would like to have

expect(shallow(<Component />).find('SubComponent')).to.be.equalsJSX(<SubComponent foo="bar" />)

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
veslncommented, Jan 30, 2016

+1 for .to.equal

0reactions
CzBuCHicommented, Mar 23, 2017

i had same issue and this is my solution for it:

const ReactElementToString = require('react-element-to-string');
chai.use(function(_chai, utils) {
    var Assertion = _chai.Assertion;
    utils.addMethod(_chai.Assertion.prototype, 'equalJsx', function (expected) {
        // TODO: check that this._obj is ShallowWrapper instance 
        // i dont know how to get ShallowWrapper type here... (im using typescript)
        // new Assertion(this._obj).to.be.instanceof(ShallowWrapper);
        const actualString = ReactElementToString(this._obj.instance().render());
        const expectedString = ReactElementToString(expected);
        expect(actualString).to.be.equal(expectedString);
    });
});

usage:

// components
function Foo({ name, value }) { return <div className="Foo"><Bar name={name} value={value} /></div>; }
function Bar({ name, value }) { return <div className="Bar"><Baz name={name} value={value} /></div>; }
function Baz({ name, value }) { return <div className="Baz">{name} - {value}</div>; }

// and inside describe-it:
const actual = shallow(<Foo name="Foo" value="Bar" />);
const expected = <div className="Foo"><Bar name="Foo" value="Foo" /></div>;
expect(actual).to.equalJsx(expected);

and nice error message:

-<div className="Foo">
-  <Bar name="Bar" value="Bar" />
+<div className="Foo2">
+  <Bar name="Foo" value="Foo" />
</div>

changed code in package react-element-to-string - props wasnt sorted (not nesseseary - props order is irelevant but if changed tests would fail…)

file: index.js

-26: return Object.keys(element.props || {}).map(function(prop) {
+26: var keys = Object.keys(element.props || {});
+27: keys.sort();
+28: return keys.map(function(prop) {

cheers

edit: sorry for thread necromancy 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better assertions for shallow-rendered React components
Another particularly useful approach is to find nodes in the rendered tree which are equal to an expected node, specified by the test....
Read more >
Shallow Renderer - React
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 >
Shallow Rendering API - Enzyme - GitHub Pages
Returns whether or not one of the given react elements exists in the shallow render tree. .equals(node) => Boolean. Returns whether or not...
Read more >
Better assertions for shallow-rendered React components | by ...
The React TestUtils shallow rendering feature allows us to test React components in true isolation from other component classes, and removes the need...
Read more >
Shallow rendering using enzyme simple test not working
You're asserting that an enzyme ShallowWrapper is equal to 3 or 0. This doesn't make sense. Instead, the ShallowWrapper that is returned ...
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