Power of Jest Snapshot

栏目: IT技术 · 发布时间: 5年前

内容简介:Like the name says, it’s basically a snapshot of output, you want to compare over time and test if the code is up to the expectations. For example, let’s say you have written a React component which renders some UI. Over the time, different people work on

Jest is a JavaScript testing framework built by Facebook . It has the test runner, test evaluator and assertions utilities built in. It’s an amazing framework to use for testing your applications, may it be written in React , Angular or Vue . In this article, I will explain what is Snapshot feature of Jest and show an interesting use case which can make your testing simpler and faster.

What is Snapshot?

Like the name says, it’s basically a snapshot of output, you want to compare over time and test if the code is up to the expectations. For example, let’s say you have written a React component which renders some UI. Over the time, different people work on it and change the UI output of it. This is stored as a text as shown below and a simple diff comparison of the text outputs can tell if the code is giving the correct output. The main interest of this feature is to make testing easier and painless, testing the goals of your App to render certain UI and not to worry about the implementations.

Though this snapshotting is widely used for testing React components, you can use to test the any output that can be serializable , for example objects or json.

The Case

Lets say, you have a configuration file exporting different values as one below:

// config.js

export const APP_NAME = 'MY_APP';

export const APP_MAIN_ROUTE = 'app/main';

export const APP_USER_ROUTE = 'app/user';

Ealier, I used to write a test, which used to import all these params and do individual assertions, or write some wrapper logic to get it working with a single assertion. But with Jest snapshot, it’s way simplified, as one below:

// config.test.js

import * as constants from '<source-file-path>';

test('constants matches snapshot', () => {
  expect(constants).toMatchSnapshot();
});

On the first run, it will create a snapshot file, as shown below and compare against it on future runs.

// config.test.js.snap
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`constants matches snapshot 1`] = `
Object {
  "APP_NAME": "MY_APP",
  "APP_MAIN_ROUTE": "app/main",
  "APP_USER_ROUTE": "app/user",
}
`;

If you have a very huge, nested object (probably a configuration object), you can test it via snapshotting with a single line . A lot of assertion or implementation related coding can be avoided. Make sure to give this a try and let me know your thoughts in comments.

Thanks a lot for reading through. Please let me know how you felt and if you would like to hear more on any topic, don’t forget to let me know in comments. Have a good day!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机科学概论(第11版)

计算机科学概论(第11版)

J. Glenn Brookshear / 刘艺、肖成海、马小会、毛倩倩 / 人民邮电出版社 / 2011-10-1 / 69.00元

本书多年来一直深受世界各国高校师生的欢迎,是美国哈佛大学、麻省理工学院、普林斯顿大学、加州大学伯克利分校等许多著名大学的首选教材,对我国的高校教学也产生了广泛影响。 本 书以历史眼光,从发展的角度、当前的水平以及现阶段的研究方向等几个方面,全景式描绘了计算机科学各个子学科的主要领域。在内容编排上,本书很好地兼顾了 学科广度和主题深度,把握了最新的技术趋势。本书用算法、数据抽象等核心思想贯穿各......一起来看看 《计算机科学概论(第11版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器