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. PatentTips - Supporting address translation in a virtual machine environment

    BACKGROUND A conventional virtual-machine monitor (VMM) typically runs on a computer and presents to ...

  2. Python3的取余操作

    https://blog.csdn.net/u014647208/article/details/53368244 取余代码: 输入以下代码: >>>10%2 >>> ...

  3. cocos2d-x 3.0 游戏关卡滑动 弹动 不会出现黑边效果

    #pragma once #include "cocos2d.h" #include "ShopScene.h" using namespace cocos2d ...

  4. CSS笔记 - fgm练习 - 鼠标移入移出div显示隐藏 CSS样式部分

    问题总结: 1. checkbox和下面隐藏的div对齐,是在清除了默认样式的前提下,而不需要额外设置float: left; 2. 隐藏的div这里不需要专门设置宽高.居中,是靠内容和padding ...

  5. (转)如何启动或关闭数据库的归档(ARCHIVELOG)模式

    转自:http://www.eygle.com/archives/2004/10/oracle_howtoeci.html Oracle数据库可以运行在2种模式下:归档模式(archivelog)和非 ...

  6. 常用到的Linux命令

    记录一下日常用到的Linux命令,就当做日志了 1.查看Linux 端口号  netstat -apn | grep 80 2.杀死进程   kill -s 9 pid (tomcat 启动不起来有可 ...

  7. 大战C100K之-Linux内核调优篇--转载

    原文地址:http://joyexpr.com/2013/11/22/c100k-4-kernel-tuning/ 早期的系统,系统资源包括CPU.内存等都是非常有限的,系统为了保持公平,默认要限制进 ...

  8. 【例题 6-2 UVA - 514】Rails

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 栈模拟一下就好. 每个输出段后面都有一个空行. 包括最后一个. [代码] #include <bits/stdc++.h> ...

  9. WPF中实现验证码

    原文:WPF中实现验证码 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79563449 W ...

  10. 轻松掌握ISO8583报文协议

    http://www.itpub.net/thread-419521-1-1.html 我刚进入金融行业时,就知道了IS08583报文协议,我想可能我还没进入这个行业都已经听过了,可知ISO8583的 ...