Shallow `contain` fails
See original GitHub issueThe 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:
- Created 7 years ago
- Reactions:1
- Comments:12 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
After: https://github.com/ayrton/chai-enzyme-issue-92/commit/5511d692ae18e987cb6f22f8438052baf49f2700
After re-installing my
node_modules
, I’ve simplified the example down to:which I run with
and I get the same results. The
.to.contain
test fails.