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的更多相关文章

  1. Python DDT(data driven tests)模块心得

    关于ddt模块的一些心得,主要是看官网的例子,加上一点自己的理解,官网地址:http://ddt.readthedocs.io/en/latest/example.html ddt(data driv ...

  2. Spock - Document - 03 - Data Driven Testing

    Data Driven Testing Peter Niederwieser, The Spock Framework TeamVersion 1.1 Oftentimes, it is useful ...

  3. 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 ...

  4. [转]Table-Driven and Data Driven Programming

    What is Table-Driven and Data-Driven Programming? Data/Table-Driven programming is the technique of ...

  5. [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 ...

  6. [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 ...

  7. Rails 5 Test Prescriptions 第6章Adding Data to Tests

    bcreate the data quickly and easily.考虑测试运行的速度. fixtures and factories.以及下章讨论的test doubles,还有原生的creat ...

  8. [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 ...

  9. Quality in the Test Automation Review Process and Design Review Template

    About this document Prerequisite knowledge/experience: Software Testing, Test Automation Applicable ...

随机推荐

  1. Java使用Cipher类实现加密,包括DES,DES3,AES和RSA加密

    一.先看一个简单加密,解密实现 1.1 加密 /** * content: 加密内容 * slatKey: 加密的盐,16位字符串 * vectorKey: 加密的向量,16位字符串 */ publi ...

  2. 【BZOJ2565】最长双回文串 (Manacher算法)

    题目: BZOJ2565 分析: 首先看到回文串,肯定能想到Manacher算法.下文中字符串\(s\)是输入的字符串\(str\)在Manacher算法中添加了字符'#'后的字符串 (构造方式如下) ...

  3. mariadb的安装

    mysql (分支 mariadb)1.安装mariadb -yum -源码编译安装 -下载rpm安装 yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./co ...

  4. Spring思维课程导图——bean属性的设置

  5. 【转】Linux GCC常用命令

    转自:http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html 1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Comp ...

  6. 15年用canvas画的

    请恕我当年的工作太轻松,用canvas手打了一个图,技术含量并没有什么,现在看看,甚是怀念_(¦3」∠)_ <!DOCTYPE html> <html> <head&g ...

  7. git怎么克隆远程仓库到本地仓库

    参考: https://blog.csdn.net/zhangzeshan/article/details/81564990 不知道为什么输入git的克隆地址就会提示密码错误 ,使用http地址就直接 ...

  8. .net 内嵌 GeckoWebBrowser (firefox) 核心浏览器

    引用nuget包: 注意:Geckofx45 nuget包必须是最后引用,否则初始化会出错 简单示例: using Gecko; using System; using System.Collecti ...

  9. PAT_A1066#Root of AVL Tree

    Source: PAT A1066 Root of AVL Tree (25 分) Description: An AVL tree is a self-balancing binary search ...

  10. openstack——cinder服务篇

    一.cinder 介绍:     理解 Block Storage 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文 ...