创建快照时,Jest / Enzyme ShallowWrapper为空

dra*_*agi 13 testing snapshot reactjs jestjs enzyme

所以我正在为我的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();
  });
});
Run Code Online (Sandbox Code Playgroud)

它创建的快照:

// Jest Snapshot v1

exports[`<ItemCard/> renders and matches the snapshot 1`] = `ShallowWrapper {}`;
Run Code Online (Sandbox Code Playgroud)

据我所知ShallowWrapper应该在其中包含一些内容,而不是为空。

有人可以告诉我我在做什么错吗?

谢谢

Uri*_*ell 21

应该不需要恢复版本。遵循官方DOC

您需要将此添加到您的Jest 配置中

"snapshotSerializers": ["enzyme-to-json/serializer"]
Run Code Online (Sandbox Code Playgroud)

线索:可以像将其添加到您的package.json一样简单,例如:

{
  "name": "my-project",
  "jest": {
    "snapshotSerializers": ["enzyme-to-json/serializer"]
  }
}
Run Code Online (Sandbox Code Playgroud)

对不起,如果这不是答案。我只是看到这里没有人告诉它,几分钟前它必须帮助像我这样的其他迷路者。


Tit*_*nis 17

对于Jest v24,您需要使用快照序列化程序,例如https://github.com/adriantoine/enzyme-to-json

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


use*_*429 8

更新到 jest@24.0.0 后我遇到了同样的问题我暂时恢复到以前的版本 jest@23.6.0,直到我弄清楚发生了什么变化。如果您发现有什么变化,请在此处发布。

  • 如果您使用的是版本 7,则恢复到 `Jest 23` 会完全破坏 Babel,因为它试图调用版本 6。 (3认同)