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.

  1. const { createPerson } = require('./index')
  2.  
  3. describe('createPerson', () => {
  4. it('Creates a person object', () => {
  5. const result = createPerson('John', 'Smith')
  6. expect(result).toMatchSnapshot({
  7. id: expect.any(String),
  8. createdAt: expect.any(Date)
  9. })
  10. })
  11. })

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:

  1. expect(result).toMatchSnapshot({
  2. id: expect.any(String),
  3. createdAt: expect.any(Date)
  4. })

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. C#模拟百度登录并到指定网站评论回帖(二)

    序言: 回归正题:前面讲到的抓包分析的数据,是模拟登录要获得得必要信息(当然有些也不是必要的...我只是都列举出来这样有个对比)如果说,有哪个英文字母不知道什么意思的,可以问一下度娘,有不少前辈都发过 ...

  2. Ubuntu 系统的常用快捷键

    Ubuntu操作基本快捷键 ibus-setup :设置系统输入法 scp filename username@serverIp:/home/xxx/xxx/filename   回车输入该usern ...

  3. fcc 响应式框架Bootstrap 练习1

    需要通过添加下列代码到你的HTML开头来将Bootstrap添加到任意应用中: <link rel="stylesheet" href="//cdn.bootcss ...

  4. Java Web框架前景浅析

    基于三(多)层架构模式,典型WEB系统的总体架构如下图所示: 在上述分层架构中,整个应用被划分为两大部分: 客户端:基于浏览器提供信息展现.用户交互等功能.所采用的技术主要有:HTML/HTML5.J ...

  5. AjaxDemo

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. XML在线转化为JSON

    http://www.utilities-online.info/xmltojson/

  7. 吐得了,vue的多选组合框回显必须是字符串集合

    下面这个typeIdList,如果给他赋值,就能回显到页面,但是必须是字符串的集合,如果是数值类型的id,不好意思,请转成字符串

  8. 用C#在Visual Studio写Javascript单元测试(Firefox内核)

    引用nuget包: 注意:Geckofx45 nuget包必须是最后引用,否则初始化会出错 编写JsRunner using Gecko; using System; using System.Col ...

  9. 怎么选择最适合自己的Linux培训机构?

    Linux培训已经成为入门Linux的一个重要途径,它的优势在于学习知识的系统性.快速性和实用性.Linux培训毕业的学员大多数拥有较强的实战动手能力,能够较快上手,更符合企业需求. 不过,大部分同学 ...

  10. 刽子手游戏(Hangman Judge, UVa 489)

    刽子手游戏其实是一款猜单词游戏,游戏规则是这样的:计算机想一个单词让你猜,你每次可以猜一个字母.如果单词里有那个字母,所有该字母会显示出来:如果没有那个字母,则计算机会在一幅“刽子手”画上填一笔.这幅 ...