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的更多相关文章

  1. ava 类似jest snapshot 功能试用

    ava也提供了类似jest 的snapshot 测试,可以用来方便的测试web 组件,以下是一个简单的试用, 同时包含了自己碰到问题,以及解决方法,以及一些参考链接 使用typescript 以及ts ...

  2. [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 ...

  3. [Testing] Config jest to test Javascript Application -- Part 1

    Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...

  4. [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 ...

  5. 前端测试框架Jest系列教程 -- Mock Functions

    写在前面: 在写单元测试的时候有一个最重要的步骤就是Mock,我们通常会根据接口来Mock接口的实现,比如你要测试某个class中的某个方法,而这个方法又依赖了外部的一些接口的实现,从单元测试的角度来 ...

  6. 搭建 Jest+ Enzyme 测试环境

    1.为什么要使用单元测试工具? 因为代码之间的相互调用关系,又希望测试过程单元相互独立,又能正常运行,这就需要我们对被测函数的依赖函数和环境进行mock,在测试数据输入.测试执行和测试结果检查方面存在 ...

  7. 前端测试框架Jest系列教程 -- Mock Functions(模拟器)

    写在前面: 在写单元测试的时候有一个最重要的步骤就是Mock,我们通常会根据接口来Mock接口的实现,比如你要测试某个class中的某个方法,而这个方法又依赖了外部的一些接口的实现,从单元测试的角度来 ...

  8. jest js 测试框架-简单方便人性化

    1. 安装 yarn global add jest-cli or npm install -g jest-cli 备注:可以安装为依赖不用全局安装 2. 项目代码 a. 项目初始化 yarn ini ...

  9. [React & Testing] Snapshot testings

    For example we have a React comonent: -- A toggle button, we want to test. When it si toggle on, the ...

随机推荐

  1. HDU 1007 Quoit Design 平面内最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...

  2. Linux启动(续)

    runlevel (启动级别):    查看命令 :who -r 或 runlevel         0:halt 关机         1:单用户模式,直接以管理员身份登录,不需要密码       ...

  3. CISP/CISA 每日一题 16

    CISA 每日一题(答) 作业调度软件的优点: 1.作业信息仅需建立一次,减少错误发生概率: 2.可定义作业间的依赖关系,当某一项作业失败时,依赖于该作业的后续作业就不会被执行: 3.所有成功或失败的 ...

  4. 洛谷 P1724 东风早谷苗

    洛谷 P1724 东风早谷苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走, ...

  5. 如何在同一台机器上安装多个MySQL的实例(转)

    最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的MySQL的实例). 先说下,什么是mysql的多实例,简单的来说就是一台机器上安装了多个mysql的服务 ...

  6. js进阶 14-8 表单序列化函数serializeArray()和serialize()的区别是什么

    js进阶 14-8 表单序列化函数serializeArray()和serialize()的区别是什么 一.总结 一句话总结:两者都是对表单进行序列化,serializeArray()返回的是json ...

  7. Core Animation 文档翻译—附录B(可动画的属性)

    前言   许多CALayer和CIFliter的属性都是可动画的.本节附录列出了这些属性默认使用的动画.   CALayer可动画属性   表B-1展示了CALayer类的可动画属性.针对每个属性此表 ...

  8. SqlMapConfig.xml全局配置文件解析(mybatis)

    原文  http://www.cnblogs.com/selene/p/4607004.html 一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) set ...

  9. angular设置全局变量,可修改监听变量

    创建service.module.ts import { NgModule, ModuleWithProviders } from '@angular/core'; import { SomeShar ...

  10. 水题ing

    T1: https://www.luogu.org/problemnew/show/P1724幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆 ...