vitest/consistent-each-for

Enforce either .each or .for for parameterized tests

Vitest has two ways to run a test once per row of data. test.each spreads an array row into separate arguments. test.for passes the row as one argument. When a project mixes them, you have to check which one a test uses before you can read its callback's parameters.

The rule does nothing until you choose a method for at least one function.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"vitest/consistent-each-for" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"vitest/consistent-each-for" = ["error", {
  # test = <value>,
  # it = <value>,
  # describe = <value>,
  # suite = <value>,
}]

Examples#

bunfig.toml
[lint.rules]
"vitest/consistent-each-for" = ["error", { test = "for" }]
example.tswith { "test": "for" }1 problem
import { expect, test } from "vitest";

test.each([
  [1, 2, 3],
  [2, 3, 5],
])("adds %i and %i", (a, b, sum) => {
  expect(a + b).toBe(sum);
});
example.tswith { "test": "for" }No problems
import { expect, test } from "vitest";

test.for([
  [1, 2, 3],
  [2, 3, 5],
])("adds %i and %i", ([a, b, sum]) => {
  expect(a + b).toBe(sum);
});

Options#

Each option takes the method you want, "each" or "for". A function you leave out isn't checked.

OptionDefaultWhat it does
testnot checkedThe method for test.
itnot checkedThe method for it.
describenot checkedThe method for describe.
suitenot checkedThe method for suite.

test and it are separate settings. If you only set test, it.each is still allowed:

example.tswith { "test": "for" }No problems
import { expect, it } from "vitest";

it.each([1, 2, 3])("accepts %i", (value) => {
  expect(value).toBeGreaterThan(0);
});