博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Jest] Use property matchers in snapshot tests with Jest
阅读量:5899 次
发布时间:2019-06-19

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

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.

转载地址:http://eohsx.baihongyu.com/

你可能感兴趣的文章
Android 四大组件之一(Activity)
查看>>
扫描(一)
查看>>
PIE SDK矢量数据的读取
查看>>
两种方式分别改变alertdialog的宽和高
查看>>
TextView-setCompondDrawables用法
查看>>
淘宝Hadoop集群的概况
查看>>
Centos7安装rabbitmq server 3.6.0
查看>>
关于eclipse的ADT(插件)对xml的android:text属性检查修改
查看>>
iostat命令学习
查看>>
SQL 三种分页方式
查看>>
查看linux是ubuntu还是centos
查看>>
html video的url更新,自动清缓存
查看>>
IOS Xib使用——为控制器添加Xib文件
查看>>
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤
查看>>
react 取消 eslint
查看>>
【11】ajax请求后台接口数据与返回值处理js写法
查看>>
Python菜鸟之路:Jquery Ajax的使用
查看>>
LeetCode算法题-Maximum Depth of Binary Tree
查看>>
Vim和操作系统剪贴板交互
查看>>
Cox 教学视频5
查看>>