[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 ...
随机推荐
- vue项目使用axios
使用: npm install axios --save-dev 在main.js中import: 使用: (1):POST方式 let data= [{receiveAdd:receiveAddVa ...
- Unity新版网络-NetworkManager
NetworkManager是一个组件,用来管理网络多人游戏的状态.它实际上是完全使用HLAPI实现,所以它所做的一切程序员可以使用其他方式实现.然而,NetworkManager封装好了很多有用的功 ...
- Windows Forms 窗体篇
1,显示窗体 非模式: Form form = new Form(); form.Show(); 模式: Form form = new Form(); form.Show(); 2,拥有者窗体与附属 ...
- Android网络框架OkHttp之get请求(源码初识)
概括 OkHttp现在很火呀.于是上个星期就一直在学习OkHttp框架,虽然说起来已经有点晚上手了,貌似是2013年就推出了.但是现在它版本更加稳定了呀.这不,说着说着,OkHttp3.3版本在这几天 ...
- 【习题 6-7 UVA - 804】Petri Net Simulation
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟就好 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sever ...
- amazeui学习笔记--js插件(UI增强)--警告框Alert
amazeui学习笔记--js插件(UI增强)--警告框Alert 一.总结 1.警告框基本样式:用am-alert声明div容器, <div class="am-alert" ...
- java set转list,数组与list的转换
读zookeeper的源码(zookeeper.java)时发现的,平时没有怎么注意: private final ZKWatchManager watchManager; List<Strin ...
- Linux常用命令及解析
基本日常命令 init 3 (进入命令行页面) steup (设置网络) exit (退出用户) pwd(查看当前所在目录) date(查看当前系统时间) 举例:(date +%Y-%m-%d)以年月 ...
- AMBA标准
AMBA标准定义了三种不同的总线 高级高性能总线(AHB): 高级系统总线(ASB):-----用的比较少 高级外设总线(APB). 基于AMBA的典型微控制器: 典型的AMBA AHB系统设计包含以 ...
- [Android5.1]ActivityManagerService启动过程分析
ActivityManagerService(简称AMS)是Android系统的关键服务之中的一个.它的主要作用例如以下: 管理系统中全部应用进程的整个生命周期 管理应用进程中的Activity.Se ...