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. Jquery获取select选中的option的文本信息

    注意:以下用的$(this)代表当前选中的select框 第一种: $(this).children("option:selec... ...查看全文

  2. Android BuildConfig:Gradle自定义你的BuildConfig

    在很早之前我发布了这篇博客Android BuildConfig.DEBUG的妙用, 提到了Eclipse中通过BuildConfig.DEBUG字段用来调试Log非常好用,但是殊不知在Android ...

  3. (转)高强度密码管理软件KeePass使用详解

    转自:http://www.ruancan.com/ 算下来,你接触电脑有多久了?从第一次上网,到今天,你一共申请了多少个网站或者软件的帐号?相信这是一个几乎无人能够回答的问题. 无数人面临着这两个问 ...

  4. python3 随机生成10以内的加法算术题

    今晚晚饭过后,看到小孩在做加法题,全是10以内的,因为她现在只会10以内的加法题.而这些题是老婆手动出的题目. 看到这个情景,突然想到,可以用python来实现随机出题,而且可以指定出多少题,出多少以 ...

  5. Java FutureTask Example Program(Java FutureTask例子)

    Sometime back I wrote a post about Java Callable Future interfaces that we can use to get the concur ...

  6. NodeJS服务端重构计划

    不知不觉做node开发已经半年时间了.这期间写尝试着去攻克了一些问题.实现了一下想法,也遇到过一些坑. 是时候来梳理一下代码,规划一下接下来的工作. 现阶段我们的nodeserver端代码结构是这种: ...

  7. [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>

    Angular 1 provided a mechanism to place content from your template inside of another template called ...

  8. DateTime与timeStamp的转换

    DateTime转换为timeStamp: DateTime dt = DateTime.Now;            DateTime startTime = TimeZone.CurrentTi ...

  9. 基于Android Fragment功能的样例

    通过近期空暇时候对Fragment的学习,尝试着写了一个小Demo,将在开发的时候能经常使用到的Fragment知识放在一起,写过了这个Demo对Android Fragment的了解更加深入了,以后 ...

  10. Activity生命周期的回调,你应该知道得很多其它!--Android源代码剖析(下)

            转载请标明原文地址:http://blog.csdn.net/yalinfendou/article/details/46910811[yalinfendou的博客]          ...