[Jest] Snapshot
The problem we face daily when we do testing:
The Data structure may changing, component outlook might changing... this makes it hard for us do testing. Imaging when the data structure changed, your tests broken, when you need to change the tests accordingly. Our your component DOM structure changed, then you need to update your tests to match the changes.
There are lots of manul work. Jest's snapshot make it easy for us to do test.
You no longer need to hard code value inside your test, you just need to do:
import React from 'react';
import renderer from 'react-test-renderer';
import MovieList from './MovieList'; it('Renders movies', () => {
const component = renderer.create(
<MovieList query="F" />
); expect(component).toMatchSnapshot();
})
it will generate a snapshot file, and next time you run the tests, it will check whether the output is the same as what saved into snapshot. If they are not the same, tests will faild, but it allow you to update snapshot, if after updated, they are matched, then tests will pass.
You can also control what to be saved into snapshot by using 'Serializer'.
expect.addSnapshotSerializer({
test: (val) => val.title && val.emoji, // check whether should use serializer
print: () => `${val.emoji} ${val.title}`// the formatted value to be saved into snapshot
})
It can also work with serializer from other libaray:
import {shallow} from 'enzyme';
import enzymeSerializer from 'enzyme-to-json/serializer';
expect.addSnapshotSerializer(enzymeSerializer);
it('REnders movies', () => {
const component = shallow(
<MovieList query="F" />
);
expect(component).toMatchSnapshot();
});
[Jest] Snapshot的更多相关文章
- ava 类似jest snapshot 功能试用
ava也提供了类似jest 的snapshot 测试,可以用来方便的测试web 组件,以下是一个简单的试用, 同时包含了自己碰到问题,以及解决方法,以及一些参考链接 使用typescript 以及ts ...
- [React] Use Jest's Snapshot Testing Feature
Often when testing, you use the actual result to create your assertion and have to manually update i ...
- [Testing] Config jest to test Javascript Application -- Part 1
Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...
- [Jest] Use property matchers in snapshot tests with Jest
With the right process in place, snapshot tests can be a great way to detect unintended changes in a ...
- 前端测试框架Jest系列教程 -- Mock Functions
写在前面: 在写单元测试的时候有一个最重要的步骤就是Mock,我们通常会根据接口来Mock接口的实现,比如你要测试某个class中的某个方法,而这个方法又依赖了外部的一些接口的实现,从单元测试的角度来 ...
- 搭建 Jest+ Enzyme 测试环境
1.为什么要使用单元测试工具? 因为代码之间的相互调用关系,又希望测试过程单元相互独立,又能正常运行,这就需要我们对被测函数的依赖函数和环境进行mock,在测试数据输入.测试执行和测试结果检查方面存在 ...
- 前端测试框架Jest系列教程 -- Mock Functions(模拟器)
写在前面: 在写单元测试的时候有一个最重要的步骤就是Mock,我们通常会根据接口来Mock接口的实现,比如你要测试某个class中的某个方法,而这个方法又依赖了外部的一些接口的实现,从单元测试的角度来 ...
- jest js 测试框架-简单方便人性化
1. 安装 yarn global add jest-cli or npm install -g jest-cli 备注:可以安装为依赖不用全局安装 2. 项目代码 a. 项目初始化 yarn ini ...
- [React & Testing] Snapshot testings
For example we have a React comonent: -- A toggle button, we want to test. When it si toggle on, the ...
随机推荐
- JS和安卓 IOS的交互 例子式记录
(function () { var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexO ...
- 如何运行vue项目(维护他人的项目)
假如你是个小白,在公司接手他人的项目,这个时候,该怎么将这个项目跑通? 前提: 首先,这个教程主要针对vue小白,并且不知道安装node.js环境的.言归正传,下面开始教程:在维护项目之前,需要把所有 ...
- Android 手机插入电脑后提示“”ADB Interface"安装失败的问题
安装ADB Interface 1. Android Studio开发时,如果在真机上测试并不是那么的顺利.直接将手机插入电脑,并不能识别.往往提示ADB Interface驱动未安装.如下图所示. ...
- 【Expression 序列化】WCF的简单使用及其Expression Lambada的序列化问题初步解决方案
地址:http://www.cnblogs.com/guomingfeng/tag/Expression%E5%BA%8F%E5%88%97%E5%8C%96/
- Android RingtoneManager 铃声管理
package com.Aina.Android; import java.io.File; import android.app.Activity; import android.content.I ...
- 前端面试题(webpack)
(前端面试题大全,持续更新) webpack3升级到4为什么会提升速度? webpack优化有哪些? webpack的css-loader原理讲一下 webpack压缩js css的方法
- 4. Vue-Resource / axios 异步插件
安装 cnmp i vue-resource --save (--save 安装到dependencies下) 引用 <script src="node_modules/vue-res ...
- Invalid property 'annotatedClasses' of bean class
Invalid property 'annotatedClasses' of bean class 在整合Hibernate和Spring时出现,Invalid property 'annotated ...
- 深度解析VC中的消息
消息是指什么? 消息系统对于一个win32程序来说十分重要,它是一个程序运行的动力源泉.一个消息,是系统定义的一个32位的值,他唯一的定义了一个事件,向Windows发出一个通知,告诉应用程序某个事情 ...
- ArcGIS 10.5 新功能
ArcGIS 10.5正式发布,打造智能的Web GIS平台 2017年新年来临之际,ArcGIS 10.5正式发布. 历经几个版本,ArcGIS10.5已经革新为一个智能的以Web为中心的地理平台, ...