【发布时间】:2019-06-22 11:30:52
【问题描述】:
所以我正在为我的 Item 组件编写测试,并尝试渲染 ItemCard 组件,然后使用该包装器创建快照,但它返回一个空的 ShallowWrapper {}
更多信息请查看代码:
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();
});
});
它创建的快照:
// Jest Snapshot v1
exports[`<ItemCard/> renders and matches the snapshot 1`] = `ShallowWrapper {}`;
据我所知,ShallowWrapper 应该有一些内容而不是空的......
【问题讨论】:
-
看起来
mount而不是shallow会让你开心 -
@Alex 否。这与 mount/shallow 无关。
-
@Alez 我在测试函数组件时从
shallow更改为mount并且它通过了,我不知道为什么。
标签: reactjs testing jestjs enzyme snapshot