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. JavaScript--认识DOM

    文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码: 将HTM ...

  2. 【Codeforces576E_CF576E】Painting Edges(可撤销并查集+线段树分治)

    题目 CF576E 分析: 从前天早上肝到明天早上qwq其实颓了一上午MC ,自己瞎yy然后1A,写篇博客庆祝一下. 首先做这题之前推荐一道很相似的题:[BZOJ4025]二分图(可撤销并查集+线段树 ...

  3. [Luogu 1312] noip11 Mayan游戏

    [Luogu 1312] noip11 Mayan游戏 Problem: Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即 ...

  4. mysql多表查询 查询排序

    有 ask 问题表  和 answer回答表  回答表中的ask_id和 ask表中的id对应 1.查询 /*查询回答了的 */select a.id,a.title,count(b.ask_id) ...

  5. [转]sed常用命令总结

    转自:http://blog.chinaunix.net/uid-26963748-id-3249732.html 一.Sed简介 Sed:Stream Editor  流式编辑器 又称行编辑器,每次 ...

  6. [转]HTML5 Day 4: Add Drop Down Menu to ASP.NET MVC HTML5 Template using CSS and jQuery

    本文转自:http://pietschsoft.com/post/2010/11/17/HTML5-Day-4-Add-DropDown-Menu-ASPNET-MVC-HTML5-Template- ...

  7. Razor的使用

    Razor可以识别尖括号,且关键词是@,默认情况下会对输出的html代码进行转义 1.C#代码 用 @ 加 中括号 包起来 @{ ; i < ; i++) { <h3>C#语句块要用 ...

  8. 简单的KKL诊断线~~~自己在家都可以制作obd诊断接口了 ~~

    简单的KKL诊断线~~~自己在家都可以制作~~ 适合bmw 07年以前的车型,因为新的车型使用D-can作为诊断接口,所以不能再使用kkl诊断接口不过SB开头的宝马3系还是可以使用的 更多内容欢迎查看 ...

  9. drupal 8——图片组(list)在前台的显示顺序在登录状态和非登录状态不同

    问题描述:该页面是通过view来输出的,然而,登录状态下其页面中的图片组输出顺序是乱序的,而非登录状态下则根据id值升序输出. 原因:在原view配置页面中,没有配置默认的排序字段 解决方案:在vie ...

  10. [转]STL之vector容器详解

    vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...