jest --coverage
注: 第一次运行自动生成快照,之后运行将会和第一次的快照进行比对 expect(tree).toMatchSnapshot();
jest -u
- 将文件复制到项目的指定位置: ./tests/setup.js
- Package.json增加配置:
"jest": {
"setupFiles": [
"./tests/setup.js"
]
}
https://github.com/justinsisley/Jest-CSS-Modules
模拟实际环境,生成虚拟节点
- 子节点将会按照节点名称显示,不展开
- 并且只会运行render,没有生命周期相关方法
- 子节点将会被展开,并最终返回真实节点
- 会模拟DOM节点,并能做模拟操作
- 会模拟生命周期相关方法
- 引入类库:yarn add —dev enzyme-to-json
- package.json增加配置项
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
}
- 正常调用快照方法
import { shallow } from 'enzyme';
// 由于引用了redux的connect,此处必须这么写,跳过相关方法
const Click = require('./Click').WrappedComponent;
const component = shallow(
<Click/>
);
expect(component).toMatchSnapshot();