博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React] Use Jest's Snapshot Testing Feature
阅读量:4453 次
发布时间:2019-06-07

本文共 1714 字,大约阅读时间需要 5 分钟。

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(
); const component = renderer.getRenderOutput(); expect(component).toMatchSnapshot()});test('jsx: example2', () => { const component = renderer.create(
) 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(
); 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.

 

转载于:https://www.cnblogs.com/Answer1215/p/6375828.html

你可能感兴趣的文章
多路复用IO模型
查看>>
并发、串行、并行及多道技术原理
查看>>
hashlib、pickle、hmac、logging模块使用
查看>>
javascript常用知识点总结
查看>>
2019秋招复习笔记--数据库基本操作
查看>>
2019秋招复习笔试--手写代码
查看>>
2019秋招复习笔记--智力题
查看>>
MySQL学习笔记
查看>>
2019秋招面试复习 项目重点提问
查看>>
面试题
查看>>
DS博客作业08-课程总结
查看>>
利用Python爬虫刷店铺微博等访问量最简单有效教程
查看>>
浅谈软件测试与墨菲定律
查看>>
文件安全复制之 FastCopy
查看>>
强烈推荐美文之《从此刻起,我要》
查看>>
MYSQL中数据类型介绍
查看>>
评估软件上线标准
查看>>
敏捷开发流程
查看>>
APP兼容性测试(三)测试方案设计
查看>>
leetcode 412. Fizz Buzz
查看>>