With the right process in place, snapshot tests can be a great way to detect unintended changes in any part of your application that can be serialized. By grabbing a snapshot of your data in a known state, it takes a relatively small amount of code to check new output against your snapshot on each test run. When the output you want to test includes volatile data such as random number or dates, you end up updating your snapshot on every test run and your snapshot tests lose their value. Starting with Jest 23.0, the toMatchSnapshot method of expect allows you to define property matchers for specific keys in objects. Now we can specify that a value is of a certain type, while ignoring the specific value. In this lesson, we'll see an example of an object with non-deterministic values, and use property matchers in .toMatchSnapshot()to verify types while allowing for variation in those values.

const { createPerson } = require('./index')

describe('createPerson', () => {
it('Creates a person object', () => {
const result = createPerson('John', 'Smith')
expect(result).toMatchSnapshot({
id: expect.any(String),
createdAt: expect.any(Date)
})
})
})

Then function 'createPreson' will genearte an object with random 'id' and 'createAt' time. When we usinig .toMatchSnapshot() for random value, it wil faild at second time.

To solve the problem, we can just check the type instead of actual value:

expect(result).toMatchSnapshot({
id: expect.any(String),
createdAt: expect.any(Date)
})

It can make our test more flexable.

[Jest] Use property matchers in snapshot tests with Jest的更多相关文章

  1. 前端测试框架Jest系列教程 -- Matchers(匹配器)

    写在前面: 匹配器(Matchers)是Jest中非常重要的一个概念,它可以提供很多种方式来让你去验证你所测试的返回值,本文重点介绍几种常用的Matcher,其他的可以通过官网api文档查看. 常用的 ...

  2. [Jest] Write data driven tests in Jest with test.each

    Often, we end up creating multiple unit tests for the same unit of code to make sure it behaves as e ...

  3. Jest 学习笔记(一)之matchers

    Jest官网地址 Jest是专门被facebook用于测试包括React应用在内的所有javascript代码,Jest旨在提供一个综合的零计算的测试体验. 因为没有找到文档,基于我个人的经验,Jes ...

  4. [Testing] Config jest to test Javascript Application -- Part 1

    Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...

  5. 基于Typescript和Jest刷题环境搭建与使用

    写在前面 前几个月在公司用vue3和ts写项目,想巩固一下基础,于是我想起了去年基于JavaScript和Jest搭建的刷题环境,不如,给它搞个加强版,结合Typescript和Jest 搞一个刷题环 ...

  6. 前端测试框架Jest系列教程 -- Mock Functions

    写在前面: 在写单元测试的时候有一个最重要的步骤就是Mock,我们通常会根据接口来Mock接口的实现,比如你要测试某个class中的某个方法,而这个方法又依赖了外部的一些接口的实现,从单元测试的角度来 ...

  7. 前端测试框架Jest系列教程 -- Global Functions(全局函数)

    写在前面: Jest中定义了很多全局性的Function供我们使用,我们不必再去引用别的包来去实现类似的功能,下面将列举Jest中实现的全局函数. Jest Global Functions afte ...

  8. 前端测试框架Jest系列教程 -- Mock Functions(模拟器)

    写在前面: 在写单元测试的时候有一个最重要的步骤就是Mock,我们通常会根据接口来Mock接口的实现,比如你要测试某个class中的某个方法,而这个方法又依赖了外部的一些接口的实现,从单元测试的角度来 ...

  9. jest js 测试框架-简单方便人性化

    1. 安装 yarn global add jest-cli or npm install -g jest-cli 备注:可以安装为依赖不用全局安装 2. 项目代码 a. 项目初始化 yarn ini ...

随机推荐

  1. 洛谷2019 3月月赛 T3

    题干 唯一AC T3 的大巨佬%%% 这题就是个大模拟吧. 题解

  2. [转]Android | Simple SQLite Database Tutorial

    本文转自:http://hmkcode.com/android-simple-sqlite-database-tutorial/ Android SQLite database is an integ ...

  3. Mysql(Innodb)如何避免幻读

    Mysql(Innodb)如何避免幻读 有意思 MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. 间隙锁(Gap Lock):锁定 ...

  4. 【反射】Java反射机制

    Class 1.Class是一个类,一个描述类的类(也就是描述类本身),封装了描述方法的Method,描述字段的Filed,描述构造器的Constructor等属性    2.对象照镜子后(反射)可以 ...

  5. JS——预解析

    1.排查语法错误 <script> console.log(1; </script> 2.变量提升和函数整体提升 <script> console.log(n1); ...

  6. CSS——◇demo

    核心思想:嵌套盒子中的◇超过父盒子的部分隐藏. 第一种写法: <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  7. postgresql用sql语句查询表结构

    用到的postgresql系统表 关于postgresql系统表,可以参考PostgreSQL 8.1 中文文档-系统表. pg_class 记录了数据库中的表,索引,序列,视图("关系&q ...

  8. [Windows Server 2012] 杰奇CMS安全设置

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:JIEQI ...

  9. 【转载】HTTP 基础与变迁

    原文地址:https://segmentfault.com/a/1190000006689489 HTTP HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于 ...

  10. SWING界面

    import java.awt.FlowLayout;import javax.swing.*;import java.awt.Container; public class kk extends J ...