[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 expected with varied input. This is a good practice, but it can be a little tedious to create what is essentially the same test multiple times. We copy a test, paste it and update the variables that matter. Maybe we do that several times. Then, if we need to update our tests, we update each copy of the test. The good news is, starting with version 23
of Jest, there is built-in support for creating data-driven tests. In this lesson we'll take a handful of unit tests and reduce them down to a single, data-driven test with the new test.each
method in Jest. We'll also look at the alternate version of test.each
that lets us use a tagged template literal to describe the data for our test.
Additional info:
In the Jest docs: https://facebook.github.io/jest/docs/en/api.html#testeachtable-name-fn
Jest cheatsheet: https://github.com/sapegin/jest-cheat-sheet#data-driven-tests-jest-23
- describe('isPalindrome - each', () => {
- const data = [
- ['Racecar', true],
- ['Typewriter', false],
- ['rotor', true],
- ['kayak', true],
- ['Step on no pets', true]
- ];
- test.each(data)('isPalindrome(%s) --- %s', (input, expected) => {
- const result = isPalindrome(input);
- expect(result).toBe(expected)
- })
- })
- describe('isPalindrome - each template literal', () => {
- test.each`
- input | expected
- ${'Racecar'} | ${true}
- ${'Typewriter'} | ${false}
- ${'rotor'} | ${true}
- `('isPalindrome("$input") - $expected', ({ input, expected }) => {
- const result = isPalindrome(input)
- expect(result).toBe(expected)
- })
- })
[Jest] Write data driven tests in Jest with test.each的更多相关文章
- Python DDT(data driven tests)模块心得
关于ddt模块的一些心得,主要是看官网的例子,加上一点自己的理解,官网地址:http://ddt.readthedocs.io/en/latest/example.html ddt(data driv ...
- Spock - Document - 03 - Data Driven Testing
Data Driven Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Oftentimes, it is useful ...
- What is Data Driven Testing? Learn to create Framework
What is Data Driven Testing? Data-driven is a test automation framework which stores test data in a ...
- [转]Table-Driven and Data Driven Programming
What is Table-Driven and Data-Driven Programming? Data/Table-Driven programming is the technique of ...
- [Jest] Use property matchers in snapshot tests with Jest
With the right process in place, snapshot tests can be a great way to detect unintended changes in a ...
- [Jest] Track project code coverage with Jest
Jest comes pre-packaged with the ability to track code coverage for the modules you're testing, but ...
- Rails 5 Test Prescriptions 第6章Adding Data to Tests
bcreate the data quickly and easily.考虑测试运行的速度. fixtures and factories.以及下章讨论的test doubles,还有原生的creat ...
- [D3] Start Visualizing Data Driven Documents with D3 v4
It’s time to live up to D3’s true name and potential by integrating some real data into your visuali ...
- Quality in the Test Automation Review Process and Design Review Template
About this document Prerequisite knowledge/experience: Software Testing, Test Automation Applicable ...
随机推荐
- 安卓UI自适应性
出于安卓的碎片化原因,针对不同屏幕大小,最好是做到以下几点: 1.能用相对布局的就不用绝对布局,尽量使用权重,weight设置,相对布局和线性布局同条件情况下,优先选线性布局 2.在res目录下创建不 ...
- 涨知识 - II
计算机网络相关 1.在无盘工作站向服务器申请IP地址时,使用的是( )协议. A.ARP B.RARP C.ICMP D.IGMP 解析: ARP(地址解析协议)是设备通过自己知道的IP地址来 ...
- brew update失败提示:/System/Library/Frameworks/Ruby.framework/的解决方法
本文由@ray 出品,转载请注明出处. 文章链接:http://www.cnblogs.com/wolfray/p/8040701.html 想用brew安装wget,但是提示失败,然后想先 bre ...
- iis设置404错误页,返回500状态码
一般在II6下,设置自定义404错误页时,只需要在错误页中选择自定义的页面,做自己的404页面即可.但是在IIS7.0及以上时,设置完404错误页后,会发现状态码返回的是500,并且可能会引起页面乱码 ...
- 清瘦的记录者: 一个比dbutils更小巧、好用的的持久化工具
https://gitee.com/bitprince/memory 1. 概述 1.1 连接.语句和结果集 从JDBC的规范上看,其对数据访问层有相当简洁的抽象:1.连接(connection) 2 ...
- MySQL 执行计划中Extra的浅薄理解
1.using where: Extra中出现“Using where”,通常来说,意味着全表扫描或者在查找使用索引的情况下,但是还有查询条件不在索引字段当中. 如果需要回表也是用这个. 2.usin ...
- 从0开始复习JS---1、函数复习
1. 写一个函数,实现对数字数组的排序. function get_order(array){ for(var i = 0; i <array.length-1; i++){ for(var j ...
- 数据结构与算法(4) -- list、queue以及stack
今天主要给大家介绍几种数据结构,这几种数据结构在实现原理上较为类似,我习惯称之为类list的容器.具体有list.stack以及queue. list的节点Node 首先介绍下node,也就是组成li ...
- HUD 1426 Sudoku Killer (DFS)
链接 : Here! 思路 : 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数 ...
- [JS]window.location获取url各项参数详解
window.location方法后还还可以带href,search等参数,下面我们来看看获取url各项参数的办法. URL即:统一资源定位符 (Uniform Resource Locator, U ...