I'm wondering if React fragments are compatible with Enzyme's snapshots. Right now it seems like fragments from React 16+ are being rendered as Symbols in enzyme's shallow()
method, which is causes a conversion error:"TypeError: Cannot convert a Symbol value to a string"
.
Here's my test:
it('should render successfully', () => {
const wrapper = shallow(
<Sections
data={mappedMockData}
sections={mappedMockData.sections}
eligibility={mockEligibility}
/>
);
console.log(wrapper.debug());
expect(wrapper).toMatchSnapshot();
And here's the output from my console.log:
<Symbol(react.fragment)>
<div className="content-container">
<div className="flex">
<div className="sections-wrap">
<Connect(Section1) />
<Connect(Section2) />
</div>
<Connect(Section3) />
</div>
</div>
<div className="content-container">
<Connect(Section4) />
<Connect(Section5) />
</div>
</Symbol(react.fragment)>
Worth noting: I've already updated my Enzyme and React, and am already using an adapter, as suggested by enzyme's migration guide. I haven't been able to find other similar issues to this on stack overflow or Github. Thanks in advance for any insights!
Therefore, React fragments are better than the 'div' tags. With React Fragment, you can render multiple elements of a component without adding extra div tags. We can write cleaner, more readable code with React Fragments. It takes up less memory and renders components faster. Each component is rendered as expected.
No, you can't. As per the React docs on Fragments: key is the only attribute that can be passed to Fragment.
Snapshot testing has been created due to the need for an easier way to write tests for React components. Many React developers reported that they spend more time writing tests than the actual component. Therefore, snapshot testing allows React developers to quickly generate tests using its simple syntax.
Why do we use fragments in React? React fragments serve as a cleaner alternative to using unnecessary divs in our code. These fragments do not produce any extra elements in the DOM, which means that a fragment's child components will render without any wrapping DOM node.
This enzyme issue comment was helpful for a valid test. (The whole thread is useful for knowing more about the status of fragments with enzyme snapshot testing.) But this code worked for me, and outputs <Unknown></Unknown>
in the snapshot's template literal in the place of <React.Fragment>
:
const component = shallow(<FragmentComponent />)
const fragment = component.instance().render()
expect(shallow(<div>{fragment}</div>).getElement()).toMatchSnapshot()
Here is sample output:
exports[`<FragmentComponent /> it renders to match the snapshot 1`] = `
<div>
<Unknown>
<div></div>
<div></div>
</Unknown>
</div>
`;
UPDATE (JULY 2018):
Jest Version 23.0.0 (https://github.com/facebook/jest/blob/master/CHANGELOG.md#2300) added support for rendering <React.Fragment>
in snapshots with https://github.com/facebook/jest/pull/5816/files#diff-d7b82d8b858fb8e0b01a91859362b75a.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With