reactjs - 创建快照时 Jest/Enzyme ShallowWrapper 为空

标签 reactjs testing jestjs enzyme snapshot

所以我正在为我的 Item 组件编写测试,我尝试渲染 ItemCard 组件,然后使用该包装器创建快照,但它返回一个空的 ShallowWrapper {}/

更多信息请查看代码:

项目.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 中应该有一些内容,而不是空的......

最佳答案

对于 jest v24,你需要使用像 https://github.com/adriantoine/enzyme-to-json 这样的快照序列化器。

来源:https://github.com/facebook/jest/issues/7802

关于reactjs - 创建快照时 Jest/Enzyme ShallowWrapper 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54419342/

相关文章:

java - 有没有一种简单的方法可以在 Spring Boot 中使用 Java 注解从测试覆盖率中排除类?

javascript - 如何在 typescript 中扩展 React 类

python - “webpack_loader”不是已注册的标签库

reactjs - React - Jest : No tests found, 以代码 0 退出

ruby-on-rails - Rails 测试单元 : How to find an unused id for a model

jestjs - Mock.mockImplementation() 不工作

reactjs - 防止状态重置

testing - 是否可以防止特定 iframe 中的 testcafe/hammerhead 脚本注入(inject)?

javascript - NPM 包模块中的 Jest 单元测试失败并显示 `ReferenceError`

javascript - 开 Jest `toMatchSnapshot` 原因 "Maximum call stack size exceeded"