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

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

  2. react 单元测试 (jest+enzyme)

    为什么要做单元测试 作为一个前端工程师,我是很想去谢单元测试的,因为每天的需求很多,还要去编写测试代码,感觉时间都不够用了. 不过最近开发了一个比较复杂的项目,让我感觉一旦项目大了.复杂了,而且还是多 ...

  3. react typescript jest config (一)

    1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...

  4. react: typescript jest && enzyme

    Install Jest 1.install jest dependencies jest @types/jest ts-jest -D 2.jest.config.js module.exports ...

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

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

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

  7. [Jest] Snapshot

    The problem we face daily when we do testing: The Data structure may changing, component outlook mig ...

  8. ava 类似jest snapshot 功能试用

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

  9. 搭建 Jest+ Enzyme 测试环境

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

随机推荐

  1. ecshop微信接口基础认识

    ecshop微信接口基础认识,当你要学习ecshop和微信整合的时候,你就必须研究ecshop的数据结构对接以及微信数据接口的基本知识.我们知道微信其实就是通过有效的消息推送,用JSON格式的数据或者 ...

  2. [React & Testing] Snapshot testings

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

  3. 网页设计实战3 ufo类型的科技网页如何实现

    网页设计实战3 ufo类型的科技网页如何实现 一.总结 一句话总结:基础的图片素材就是如何几张图片,这个效果只是通过jquery或者js让那个png图片旋转起来了,如此而已.其实核心就是一个trans ...

  4. FFmpegh.264解码

    - (int)DecodeH264Frames: (unsigned char*)inputBuffer withLength:(int)aLength { ; ; av_init_packet(&a ...

  5. rhel5安装 oracle11

    readhat 安装11gr2文档 需要注意的地方:必须关掉的 1,防火墙:2,SElinux . root 用户运行  setup  命令可关防火墙与SElinux 修改网络配置文件,一定要重启此文 ...

  6. 一个简单http请求的jmeter压测实战流程

    1.新建线程组 2.创建http请求 注意:接口路径中的参数值要写变量 3.创建txt文件,存多个参数值 4.创建csv文件,在csv中上传txt文件 5.variable name填写txt中参数值 ...

  7. PatentTips - Resource partitioning and direct access utilizing hardware support for virtualization

    BACKGROUND The present disclosure relates to the resource management of virtual machine(s) using har ...

  8. React-Native_02:语法篇

    1.简单介绍 ECMAScript 6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式公布了.它的目标.是使得JavaScript语言能够用来编写复杂的大型应用程 ...

  9. js 小数乘积位数太长

    function accMul(arg1,arg2) { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(".& ...

  10. 【BZOJ 3172】单词

    [链接]h在这里写链接 [题意]     给你n个单词;     这n个单词组成了一篇文章;     问你每个单词在这篇文章中出现了多少次.     其中每个单词之间用一个逗号隔开->组成一篇文 ...