Sub topic 9
Covering Many Cases with test.each
This chapter assumes you can already write tests with test, it, and describe, and that you know the common expect matchers such as toBe, toEqual, and toThrow.
Some functions need checking against many inputs. Writing a separate test block for each input repeats the same body over and over. When one case changes, you have to update every copy. test.each fixes this. You write the test body once and give it a table of data. Vitest then runs the body once per row and reports each row as its own test.
The basic form: arrays of arguments
test.each takes an array of rows. Each row is an array whose items are passed to the test function as separate arguments. You call the result with a name template and the test body.
import { test, expect } from 'vitest'
function add(a, b) {
return a + b
}
test.each([
[1, 1, 2],
[2, 3, 5],
[-1, 1, 0],
])('add(%i, %i) returns %i', (a, b, expected) => {
expect(add(a, b)).toBe(expected)
})Vitest reports three tests: add(1, 1) returns 2, add(2, 3) returns 5, and add(-1, 1) returns 0. When one row fails, the output names that exact row, so you can see which input broke.
Name placeholders
The name template uses printf-style placeholders. Vitest fills them in order from the row's values.
| Placeholder | Formats the value as |
|---|---|
| `%s` | string |
| `%i` | integer |
| `%d` | number |
| `%f` | floating-point number |
| `%j` | JSON |
| `%o` | object |
| `%#` | index of the row |
| `%%` | a literal percent sign |
Object rows for readability
If a row has more than two or three values, positional arrays get hard to read. You can use objects instead. The test body receives the whole object, and the name template refers to its properties with $name.
import { test, expect } from 'vitest'
function isValidUsername(name) {
return /^[a-z0-9_]{3,16}$/.test(name)
}
test.each([
{ input: 'ada_99', valid: true },
{ input: 'ab', valid: false },
{ input: 'Has Spaces', valid: false },
])('isValidUsername("$input") is $valid', ({ input, valid }) => {
expect(isValidUsername(input)).toBe(valid)
})describe.each works the same way. Use it when every row needs a whole group of tests rather than a single one.
Common errors
- Flat array when you meant rows.
test.each([1, 2, 3])passes one argument per test. If you meant one test with three arguments, wrap it as[[1, 2, 3]]. - Destructuring the wrong shape. Array rows are spread into separate parameters. Object rows arrive as one parameter, so write
({ input, valid }), not(input, valid). - Placeholder mismatch.
%ion1.5prints1, which makes the test name misleading. Use%dor%ffor decimals. - Branching inside the body. If the body needs
ifstatements to handle different rows, the cases are not really the same test. Split them into separate tests.
Exercise
You have function isEven(n) { return n % 2 === 0 }. Write a test.each that checks 0, 7, and -4, with test names like "isEven(7) is false".
Show answer
test.each([[0, true], [7, false], [-4, true]])('isEven(%i) is %s', (n, expected) => { expect(isEven(n)).toBe(expected) }). The %s placeholder formats the boolean as "true" or "false".
Exercise
A teammate writes test.each([{ a: 1, b: 2 }])('sum', (a, b) => expect(a + b).toBe(3)). Why does it fail, and how do you fix it?
Show answer
Object rows are passed as a single argument, so a is the whole object and b is undefined. Destructure the parameter instead: ({ a, b }) => expect(a + b).toBe(3).