Enzyme wrappers 生成快照为空

来源:5-4 价格题目列表测试分析和编写

慕斯6088333

2019-06-07

老师,您好,之前test.js 代码 按照课程上所述,如下

import React from 'react'
import { shallow } from 'enzyme'
import PriceList from '../PriceList'
import {items,categories} from '../../containers/Home'

const itemswithCategory = items.map((item) => {
    item.category = categories[item.categoryId];
    return item;
})
const props = {
    items: itemswithCategory,
    onEditItem: () => {},
    onDeleteItem: () => {},
}

let wrapper; 
describe('test PriceList component', () => {
    beforeEach(() => {
        wrapper = shallow(<PriceList {...props}/>)
    })
    it('should render the component to match snapshort',()=>{
        expect(wrapper).toMatchSnapshot()
    })
})

生成结果如下: 
//// Jest Snapshot v1, https://goo.gl/fbAQLP

//exports[test PriceList component should render the component to match snapshort 1] = ShallowWrapper {};

后来我查看了一下 自己的jest 版本和enzyme 版本。

-- react-scripts@3.0.1
  `-- jest@24.7.1
  `-- enzyme@3.10.0

发现可能是jest版本不匹配,过高。装了 https://github.com/adriantoine/enzyme-to-json

改变的代码如下

添加 import toJson from 'enzyme-to-json';
describe('test PriceList component', () => {
    beforeEach(() => {
        wrapper = shallow(<PriceList {...props}/>)
    })
    it('should render the component to match snapshort',()=>{
        expect(toJson(wrapper)).toMatchSnapshot()
    })
})

snapsot是有了。但是不知道这样tojson 会对快照有什么影响么。文档上说 Convert Enzyme wrappers to a format compatible with Jest snapshot testing.。。 为什么版本更新了反而需要compatible的东西了。
快照结果:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test PriceList component should render the component to match snapshort 1`] = `
<ul
  className="list-group list-group-flush"
>
  <li
    className="list-group-item d-flex 
 justify-content-between align-items-center"
    key="1"
  >
    <span
      className="col-1"
    >
      <Ionicon
        beat={false}
        color="#6BE61A"
        fontSize="30px"
        icon="md-restaurant"
        rotate={false}
        shake={false}
        style={
          Object {
            "backgroundColor": "#347eff",
            "padding": "5px",
          }
        }
      />
    </span>
    <span
      className="col-5"
    >
      testItem
    </span>
    <span
      className="col-2 font-weight-bold"
    >
      -
       
      200
    </span>
    <span
      className="col-2"
    >
      2018-09-10
    </span>
    <a
      className="col-1"
      onClick={[Function]}
      role="button"
      style={
        Object {
          "cursor": "pointer",
        }
      }
    >
      <Ionicon
        beat={false}
        color="#6BE61A"
        fontSize="20px"
        icon="md-create"
        rotate={false}
        shake={false}
        style={Object {}}
      />
    </a>
    <a
      className="col-1"
      onClick={[Function]}
      role="button"
      style={
        Object {
          "cursor": "pointer",
        }
      }
    >
      <Ionicon
        beat={false}
        color="#6BE61A"
        fontSize="20px"
        icon="md-trash"
        rotate={false}
        shake={false}
        style={Object {}}
      />
    </a>
  </li>
  <li
    className="list-group-item d-flex 
 justify-content-between align-items-center"
    key="2"
  >
    <span
      className="col-1"
    >
      <Ionicon
        beat={false}
        color="#6BE61A"
        fontSize="30px"
        icon="md-train"
        rotate={false}
        shake={false}
        style={
          Object {
            "backgroundColor": "#347eff",
            "padding": "5px",
          }
        }
      />
    </span>
    <span
      className="col-5"
    >
      testItem2
    </span>
    <span
      className="col-2 font-weight-bold"
    >
      +
       
      300
    </span>
    <span
      className="col-2"
    >
      2018-09-10
    </span>
    <a
      className="col-1"
      onClick={[Function]}
      role="button"
      style={
        Object {
          "cursor": "pointer",
        }
      }
    >
      <Ionicon
        beat={false}
        color="#6BE61A"
        fontSize="20px"
        icon="md-create"
        rotate={false}
        shake={false}
        style={Object {}}
      />
    </a>
    <a
      className="col-1"
      onClick={[Function]}
      role="button"
      style={
        Object {
          "cursor": "pointer",
        }
      }
    >
      <Ionicon
        beat={false}
        color="#6BE61A"
        fontSize="20px"
        icon="md-trash"
        rotate={false}
        shake={false}
        style={Object {}}
      />
    </a>
  </li>
</ul>
`;
写回答

1回答

张轩

2019-06-11

同学你好 你写的很认真 点赞

jest 24 需要 serializer 来实现这个功能,前因后果在这里 https://stackoverflow.com/a/54552699/776977,可以看看这两个帖子。

其实不需要每次都调用toJSON, jest 配置里有 serializerSnapshot 的选项,具体配置在这里 https://medium.com/trabe/snapshot-serializers-in-jest-7b38eca72201 


1
1
慕斯6088333
非常感谢!
2019-06-12
共1条回复

React16组件化+测试+全流程 实战在线账本项目

轻松上手,从设计图到上线,精通组件化思维和组件测试

713 学习 · 177 问题

查看课程