[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 ...
随机推荐
- 绑定树tree 的后台方法
#region 获取部门列表树集合 /// <summary> /// 获取部门列表树集合 /// </summary> ...
- C#中Random
说明:C#中的随机数是一个伪随机数,随机数字从一组有限的数字选择以相同的概率,所选的数字不是完全随机的,因为使用数学算法来选择它们.在大多数Windows系统中,Random的15毫秒内创建的对象很可 ...
- Linux下安装Wine 运行、卸载 windows程序
资料 首页 https://www.winehq.org/ 安装 https://www.winehq.org/download/ 教程 https://www.winehq.org/document ...
- [转]linux tr命令详解
转自:http://www.cnblogs.com/huangxingkezhan/archive/2013/01/23/2874031.html 通过使用 tr,您可以非常容易地实现 sed 的许多 ...
- InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析
InnoDB锁机制之Gap Lock.Next-Key Lock.Record Lock解析 有意思,解释的很好
- 关于MVC视图下拉菜单绑定与取值的问题
绑定视图中dropdownlist: 视图中的代码: @Html.DropDownList("select1") 此处的slect1也就是页面上的<select>< ...
- 新浪云虚拟机ftp链接显示失败问题
新浪云虚拟机ftp链接显示失败问题 测试是在局域网遇到的 域名解析可以ping有字节回复 账号密码也没有错误,但是链接一直出现 连接失败 拒接连接等问题 解决办法: 其实是局域网内的问题,这 ...
- printFinal用法示例
printFinal是一个基于jQuery的打印插件,支持打印预览,使用很简单,废话不多多说,直接上代码. <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...
- spring 415
不支持的媒体类型 spring mvc 使用@requestBody注解json请求时,jQuery有限制,否则会出现 415 错误 1.使用ajax $.ajax({ ...
- UIResponder详解
UIResponder Class Reference Managing the Responder Chain 1.- (UIResponder *)nextResponder 返回接收者的下一个相 ...