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. arm-linux-gcc 命令未找到问题

    解决方法: 1.先打开一个超级用户权限的shell: 命令: ubuntu :sudo –s centos :su - 2.在当前shell下,设置环境变量: 命令:gedit /etc/profil ...

  2. material风格前端CSS框架——Materialize

    官方网站:http://materializecss.com/(有中文,翻译不全) 中文学习站:http://www.materializecss.cn/(翻译较全)

  3. Spring CORS

    转载:Spring MVC 4.2 增加 CORS 支持 http://spring.io/blog/2015/06/08/cors-support-in-spring-framework http: ...

  4. 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…

    https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...

  5. [Angular] Style HTML elements in Angular using ngStyle

    We will learn how to make use of the ngStyle directive to directly add multiple style attributes to ...

  6. 中英文对照 —— 标点符号(punctuation)

    有限的几个: What Are the Fourteen Punctuation Marks in English Grammar? period:句号:comma:逗号:冒号:colon:分号:se ...

  7. 芯片AMS1117 电源芯片

  8. Haproxy 为 mysql 做负载均衡

    .tar.gz cd haproxy- uname -r vim /etc/haproxy.cfg global #日志 log 127.0.0.1 local0 maxconn chroot /tm ...

  9. fatfs的设置

    官方网址:file:///E:/%E5%8D%95%E7%89%87%E6%9C%BA/FATFS/ff13a/documents/doc/config.html 关于多个文件同时打开的配置::在ff ...

  10. POJ 1275 Cashier Employment 挺难的差分约束题

    http://poj.org/problem?id=1275 题目大意: 一商店二十四小时营业,但每个时间段需求的雇员数不同(已知,设为R[i]),现有n个人申请这份工作,其可以从固定时间t连续工作八 ...