Jest/Enzyme ShallowWrapper is empty when creating Snapshot

前端 未结 8 915
醉话见心
醉话见心 2020-12-28 15:25

So I\'m writing a test for my Item component and I tried to render the ItemCard component and then use that wrapper to create a snapshot but it returns an empty

8条回答
  •  无人及你
    2020-12-28 16:16

    I faced the same issue and resolved using serializer https://github.com/adriantoine/enzyme-to-json.

    npm install --save-dev enzyme-to-json

    Once installed the enzyme-to-json we can use something like below

    import React, {Component} from 'react';
    import {shallow} from 'enzyme';
    import toJson from 'enzyme-to-json';
    
    it('renders correctly', () => {
      const wrapper = shallow(
        
          Hello World!
        ,
      );
    
      expect(toJson(wrapper)).toMatchSnapshot();
    });
    
    

    The same can be resolved using shallow().debug() but prefer to use the above method.

提交回复
热议问题