Jest/Enzyme ShallowWrapper is empty when creating Snapshot

喜夏-厌秋 提交于 2019-11-30 01:34:08

问题


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 ShallowWrapper {}

Please see the code for more info:

Item.test.js

import { shallow } from 'enzyme';
import { ItemCard } from '../Item';

const fakeItem = {
  id: 'aksnfj23',
  title: 'Fake Coat',
  price: '40000',
  description: 'This is suuuper fake...',
  image: 'fakecoat.jpg',
  largeImage: 'largefakecoat.jpg',
};

describe('<ItemCard/>', () => {
  it('renders and matches the snapshot', () => {
    const wrapper = shallow(<ItemCard me item={fakeItem} showButtons />);

    // console.log(wrapper.debug());
    expect(wrapper).toMatchSnapshot();
  });
});

The snap it creates:

// Jest Snapshot v1

exports[`<ItemCard/> renders and matches the snapshot 1`] = `ShallowWrapper {}`;

As far as I know the ShallowWrapper should have some content in it instead of being empty..

Can someone tell me what am I doing wrong here?

Thanks


回答1:


I faced the same issue after updating to jest@24.0.0 I have reverted to the previous version jest@23.6.0 for the time being till i figure out what has changed. If you find what has changed, do post it here.




回答2:


For jest v24 you need to use snapshot serializer like https://github.com/adriantoine/enzyme-to-json

source: https://github.com/facebook/jest/issues/7802




回答3:


You can just use mount and debug function like this :

it('Should render Component', () => {
    const wrapper = mount(<Component {...props} />);
    expect(wrapper.debug()).toMatchSnapshot();
  });



回答4:


use debug() method after the wrapper

  it('renders and matches the snapshot', () => {
const wrapper = shallow(<ItemCard me item={fakeItem} showButtons />);

// console.log(wrapper.debug());
expect(wrapper.debug()).toMatchSnapshot(); });



回答5:


You need to use jest-enzyme like:

https://github.com/airbnb/enzyme/issues/1533#issuecomment-479591007



来源:https://stackoverflow.com/questions/54419342/jest-enzyme-shallowwrapper-is-empty-when-creating-snapshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!