我的酶快照测试结果为空

Result of my Enzyme snapshot test is empty

我对 React 中的 Jest Enzyme 测试还很陌生,我的快照测试结果是空的。

我有以下测试代码:

import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { shallow, configure } from 'enzyme';

import { Button } from '../src/components/common/Button/Button';

configure({ adapter: new Adapter() });

describe('Button', () => {
  it('should be defined', () => {
    expect(Button).toBeDefined();
  });

  it('should render correctly', () => {
    const tree = shallow(<Button />);
    expect(tree).toMatchSnapshot();
  });
});

我的快照结果:

exports[`Button should render correctly 1`] = `ShallowWrapper {}`;

我做错了什么?

我的解决方案

import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { configure, mount } from 'enzyme';
import { shallowToJson } from 'enzyme-to-json';

import { Button } from '../src/components/common/Button/Button';

configure({ adapter: new Adapter() });

describe('Button', () => {
  it('should be defined', () => {
    expect(Button).toBeDefined();
  });

  it('should render correctly', () => {
    const tree = mount(<Button />);
    expect(shallowToJson(tree)).toMatchSnapshot();
  });
});