Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest snapshot breaks with emotion 10 / babel 7 using enzyme

So I had jest snapshots working properly generating css and html in the snapshots with babel 6 / emotion 9 however I need wanted to update to babel 7 and emotion 10 but my snapshot tests with enzyme no longer work. The code compiles and works fine after updating the required code, just the tests are broken (and nothing in the migration docs show anything related to testing setup updates).

test('renders properly', () => {
  // this works generating the correct css / html snapshot output
  expect(renderer.create(<component.Template>test</component.Template>).toJSON()).toMatchSnapshot();

  //this does not
  const wrapper = shallow(<component.Template>test</component.Template>);

  expect(toJson(wrapper)).toMatchSnapshot();
});

The enzyme version generates this output:

exports[`renders properly 1`] = `
<ContextConsumer>
  <Component />
</ContextConsumer>
`;

I have tried add the emotion serializer both by adding it to the snapshotSerializers in the jest configuration and manually adding it in the setupFilesAfterEnv script.

Anyone know why I would get getting this output?

like image 862
ryanzec Avatar asked Mar 04 '19 11:03

ryanzec


1 Answers

If you have configured everything correctly just do

test('renders properly', () => {
  const wrapper = shallow(<component.Template>test</component.Template>);
  expect(wrapper).toMatchSnapshot();
});

This should work as expected.

like image 53
Mohit Yadav Avatar answered Nov 03 '22 11:11

Mohit Yadav