[React] Use Jest's Snapshot Testing Feature
Often when testing, you use the actual result to create your assertion and have to manually update it as you make changes to the feature. With Jest snapshot testing, you can let Jest do this part for you and write more tests and features faster and with more confidence. Let's learn about how you can use Jest snapshot testing to improve your own workflow.
Using different renderer lib:
import React from 'react'
import TestUtils from 'react-addons-test-utils';
import renderer from 'react-test-renderer' test('jsx', () => {
const renderer = TestUtils.createRenderer();
renderer.render(<MyComponent name="John" />);
const component = renderer.getRenderOutput();
expect(component).toMatchSnapshot()
}); test('jsx: example2', () => {
const component = renderer.create(<MyComponent name="John" />)
expect(component).toMatchSnapshot()
})
So first test eusing 'react-addons-test-utils' lib and second test using 'react-test-renderer' lib.
Sometime you might will 'expect' lib form npm, but Jest also include global 'expect' function, so to avoid conflict:
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import expectLib from 'expect';
import expectJSX from 'expect-jsx'; import Box from '../components/Box'; Object.assign({}, expect, expectLib, expectJSX); it('should rendering the string', () => {
const renderer = TestUtils.createRenderer();
renderer.render(<Box color="green" id="" />);
const result = renderer.getRenderOutput();
expect(result).toMatchSnapshot()
});
So later if you change the Box component, the test will faild. Because the snapshots are not updated, you can simply type 'u' in command line to update the snapshots, then the tests will pass.
[React] Use Jest's Snapshot Testing Feature的更多相关文章
- [Jest] Set up Testing Globals in an Application with Jest
For some React component testing, we have common setup in each test file: import { render } from 're ...
- react 单元测试 (jest+enzyme)
为什么要做单元测试 作为一个前端工程师,我是很想去谢单元测试的,因为每天的需求很多,还要去编写测试代码,感觉时间都不够用了. 不过最近开发了一个比较复杂的项目,让我感觉一旦项目大了.复杂了,而且还是多 ...
- react typescript jest config (一)
1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...
- react: typescript jest && enzyme
Install Jest 1.install jest dependencies jest @types/jest ts-jest -D 2.jest.config.js module.exports ...
- [Testing] Config jest to test Javascript Application -- Part 1
Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...
- [MST] Test mobx-state-tree Models by Recording Snapshots or Patches
Testing models is straightforward. Especially because MST provides powerful tools to track exactly h ...
- [Jest] Snapshot
The problem we face daily when we do testing: The Data structure may changing, component outlook mig ...
- ava 类似jest snapshot 功能试用
ava也提供了类似jest 的snapshot 测试,可以用来方便的测试web 组件,以下是一个简单的试用, 同时包含了自己碰到问题,以及解决方法,以及一些参考链接 使用typescript 以及ts ...
- 搭建 Jest+ Enzyme 测试环境
1.为什么要使用单元测试工具? 因为代码之间的相互调用关系,又希望测试过程单元相互独立,又能正常运行,这就需要我们对被测函数的依赖函数和环境进行mock,在测试数据输入.测试执行和测试结果检查方面存在 ...
随机推荐
- JavaScript学习总结(4)——JavaScript数组
JavaScript中的Array对象就是数组,首先是一个动态数组,无需预先制定大小,而且是一个像Java中数组.ArrayList.Hashtable等的超强综合体. 一.数组的声明 常规方式声明: ...
- CSS外边距合并(塌陷/margin越界)
原文 简书原文:https://www.jianshu.com/p/5f18f12cd162 大纲 1.什么是外边距合并?(折叠外边距) 2.外边距带来的影响 3.折叠的结果 4.产生折叠的原因 5. ...
- 00089_字节输出流OutputStream
1.字节输出流OutputStream (1)OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法: (2)输出流中定义都是写wri ...
- 洛谷 P1657 选书
P1657 选书 题目描述 学校放寒假时,信息学奥赛辅导老师有1,2,3……x本书,要分给参加培训的x个人,每人只能选一本书,但是每人有两本喜欢的书.老师事先让每个人将自己喜欢的书填写在一张表上.然后 ...
- SQL server 错误代码对比表
0 操作成功完毕. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒绝訪问. 6 句柄无效. ...
- 硬件——STM32 , 录音,wav
详细的wav头文件解析,有例子:http://www.cnblogs.com/chulin/p/8918957.html 关于录音程序的编写: 我的思路是改写原子的程序,原子的程序需要借助VS1053 ...
- (转)ORA-00257归档日志写满的解决方法
转自:http://www.cnblogs.com/xwdreamer/p/3804509.html 背景: 在前一篇博客中我们提到了如何启动或关闭oracle的归档(ARCHIVELOG)模式,在我 ...
- (转)如何启动或关闭数据库的归档(ARCHIVELOG)模式
转自:http://www.eygle.com/archives/2004/10/oracle_howtoeci.html Oracle数据库可以运行在2种模式下:归档模式(archivelog)和非 ...
- 小米R2D samba共享配置
编辑samba配置文件 vi /etc/config/samba 需要注意的是,samba有自己的配置文件 /etc/samba/smb.conf,但是修改这个文件是不生效的,这个配置文件会在重启路由 ...
- amazeui页面分析2
amazeui页面分析2 一.总结 1.弄清结构:这些部分都是一块一块分好了的,掌握结构之后,想替换哪块就替换哪块,想不要哪块就不要哪块,非常简单的 2.一块一块:替换十分简单 3.弄清楚大块之后,然 ...