adriantoine / enzyme-to-json

Snapshot test your Enzyme wrappers
MIT License
947 stars 64 forks source link

Passing into props an Array with elements wrapped in React.Fragment results in UNDEFINED nodes #97

Closed wytrych closed 6 years ago

wytrych commented 6 years ago

Test case:

import React from 'react';
import toJson from 'enzyme-to-json';
import { shallow } from 'enzyme';

const arr = [
  (
    <React.Fragment>One</React.Fragment>
  ),
  (
    <React.Fragment>Two</React.Fragment>
  ),
];

const TestComponent = function ({ options }) {
  return <div>{options[0]}</div>;
}

it('should not output UNDEFINED nodes', () => {
  const component = shallow(
    <div><TestComponent options={arr} /></div>
  );
  expect(toJson(component)).toMatchSnapshot();
});

Snapshot:


exports[`should not output UNDEFINED nodes 1`] = `
<div>
  <TestComponent
    options={
      Array [
        <UNDEFINED>
          One
        </UNDEFINED>,
        <UNDEFINED>
          Two
        </UNDEFINED>,
      ]
    }
  />
</div>
`;
adriantoine commented 6 years ago

Hi @wytrych, I'll have a look at your bug as soon as I can.

wytrych commented 6 years ago

Thank you!

adriantoine commented 6 years ago

Ok so the main isssue is actually not the array but passing a fragment as a prop, this is enough to reproduce the issue:

it('should not output UNDEFINED nodes', () => {
  const component = shallow(
    <div><TestComponent options={<React.Fragment>Test</React.Fragment>} /></div>
  );
  expect(toJson(component)).toMatchSnapshot();
});

We support Fragment as children but not (yet) as props.

adriantoine commented 6 years ago

To follow up, I opened an issue on the Jest repository and when this PR gets merged and released, it should fix your issue.

I'll keep this issue open and close it when we can test the Jest fix and make sure it solves your issue.

rickhanlonii commented 6 years ago

Verified that https://github.com/facebook/jest/pull/5816 fixes the issue:

wytrych commented 6 years ago

That's really cool, thanks for taking it further @adriantoine

adriantoine commented 6 years ago

No problem, thanks @rickhanlonii for the fix!

wytrych commented 6 years ago

I guess we still need to wait for a new version of Jest to be released and then the package version updated here?

adriantoine commented 6 years ago

@wytrych you don't need to wait for a new version of enzyme-to-json just get the next Jest release which includes @rickhanlonii's fix.

wytrych commented 6 years ago

Ok thanks!