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.

Shallow `contain` fails

See original GitHub issue

The documentation for contain states that I can assert a shallow wrapper contains a node, but this SSCE fails:

import React, {Component} from 'react';

function ChildComponent() {
  return <div className="child" />;
}

class MyComponent extends Component {
  render() {
    return (
      <div>
        <ChildComponent />
      </div>
    );
  }
}

describe('My Component', function () {
  it('renders the child', () => {
    const wrapper = shallow(<MyComponent />);
    expect(wrapper).to.contain(<ChildComponent />);
  });

  it('finds the child', () => {
    const wrapper = shallow(<MyComponent />);
    expect(wrapper.find(ChildComponent)).to.have.length(1);
  });
});

it('renders the child') throws the following error:

AssertionError: expected { Object (root, unrendered, ...) } to have a property '$$typeof'

but it('finds the child') passes.

(Note that I have expect and shallow available as globals in my Mocha test env.)

Are the docs wrong or am I using this method incorrectly?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
ayrtoncommented, Aug 2, 2016

The problem is that you didn’t set up chai-enzyme properly. I’ve created a demo repo to reproduce this.

Before: https://github.com/ayrton/chai-enzyme-issue-92/commit/1286f417df8c60887711149224587402a4665f2c

$ npm test

> chai-enzyme-issue-92@1.0.0 test /Users/adc/Developer/ayrton/chai-enzyme-issue-92
> mocha --require babel-core/register foo.test.js



  My Component
    ✓ works in enzyme
    1) but not in chai-enzyme


  1 passing (744ms)
  1 failing

  1) My Component but not in chai-enzyme:
     AssertionError: expected { Object (root, unrendered, ...) } to have a property '$$typeof'
      at Assertion.include (node_modules/chai/lib/chai/core/assertions.js:221:47)
      at Assertion.assert (node_modules/chai/lib/chai/utils/addChainableMethod.js:84:49)
      at Context.<anonymous> (foo.test.js:21:24)



npm ERR! Test failed.  See above for more details.

After: https://github.com/ayrton/chai-enzyme-issue-92/commit/5511d692ae18e987cb6f22f8438052baf49f2700

$ npm test

> chai-enzyme-issue-92@1.0.0 test /Users/adc/Developer/ayrton/chai-enzyme-issue-92
> mocha foo.test.js



  My Component
    ✓ works in enzyme
    ✓ but not in chai-enzyme


  2 passing (795ms)
0reactions
evandaviscommented, Aug 2, 2016

After re-installing my node_modules, I’ve simplified the example down to:

import React from 'react';
import {shallow} from 'enzyme';
import {expect} from 'chai';

function MyComponent() {
  return (
    <div>
      <div />
    </div>
  );
}

describe.only('My Component', function () {
  it('works in enzyme', () => {
    const wrapper = shallow(<MyComponent />);
    expect(wrapper.contains(<div />)).to.equal(true);
  });

  it('but not in chai-enzyme', () => {
    const wrapper = shallow(<MyComponent />);
    expect(wrapper).to.contain(<div />);
  });
});

which I run with

./node_modules/mocha/bin/mocha --require babel-core/register foo.test.js

and I get the same results. The .to.contain test fails.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix an error with shallow Enzyme in testing ReactJS App
I want to test if my component displayed and I have an error with shallow (see an image of error). I am using...
Read more >
React.Fragment throws an error on shallow render · Issue #2327
Enzyme seems to throw the error: ReactShallowRenderer render(): Shallow rendering works only with custom components, but the provided ele...
Read more >
enzyme.ShallowWrapper.contains JavaScript and Node.js ...
test('fails with an JSX contain expectation', t => { var actual = shallow(<Foo/>); var child = <div className="foo"/>; t.true(actual.contains(child)); });.
Read more >
Shallow Rendering API - Enzyme - GitHub Pages
.contains(nodeOrNodes) => Boolean. Returns whether or not a given node or array of nodes is somewhere in the render tree.
Read more >
Understanding the shallow function in Enzyme - Emma Goto
One of the downsides I find with mount() is that if your app's child components make some endpoint calls and/or have dependencies on...
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