[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,在测试数据输入.测试执行和测试结果检查方面存在 ...
随机推荐
- ubuntu下useradd与adduser差别,新建用户不再home文件夹下
useradd username不会在/home下建立一个目录username adduser username会在/home下建立一个目录username useradd -m username跟a ...
- CentOS搭建xfce桌面+VNC教程
CentOS搭建xfce桌面+VNC教程 Linux的安全与性能向来为开发者所称道,你可以轻松地在搜索引擎中找到各种Linux优越性的说辞,其中不乏Linux的激进者.特别是当你步入VPS领域,更多地 ...
- eclipse-hierarchyviewer 不能使用
今天安装了adt-bundle以后,发现hierarchyviewer不能用.点开了以后连手机没有效果.后来发现,还需要进入hierarchyviewer所在的sdk目录进行下权限的设置 chmod ...
- 存储过程和SQL语句比较
做为SQL存储过程和.NET的新手,下面的指导还是很有用的,自己这一段刚刚接触这些东西,搜集了一些相关的东西,能使新手较容易上手,当然啦,要精通和熟练应用,还是要看更多更深的资料的,高手请不要见笑.以 ...
- jquery点击完一个按钮,并且触发另一个按钮
$a.click(function(){ $b.trigger('click'); });
- Dotfuscator use for .net4.0 error solve
在混淆的时候报错了,错误描述大致如下: Could not find a compatible version of ildasm to run on assembly C:\xxx.dll This ...
- 深入并发AQS二
AQS须要解决下面几个问题: 1.锁状态,怎样保证并发情况下可以安全的更新? 2.当前线程不能获取锁时,放在哪里? AQS是放在一个队列其中 3.怎样提高效率? AQS的主要职责是当获取不到锁时.将线 ...
- StringBuilder和String的区别
使用 StringBuilder 语言 C# String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为 ...
- stm32的ADC左右对齐
- 9.12 Binder系统_Java实现_内部机制_Client端
Java实现中client端的RPC层(java实现)如何通过JNI来调用IPC层(C++实现)发送数据 TestServer通过addService向Service_manager注册的时候Test ...