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 problemimport { expect, test } from "vitest";
test.eachtest can not be used with .each to create parameterized test.([
[1, 2, 3],
[2, 3, 5],
])("adds %i and %i", (a, b, sum) => {
expect(a + b).toBe(sum);
});example.tswith
{ "test": "for" }No problemsimport { 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.
| Option | Default | What it does |
|---|---|---|
test | not checked | The method for test. |
it | not checked | The method for it. |
describe | not checked | The method for describe. |
suite | not checked | The method for suite. |
test and it are separate settings. If you only set test, it.each is still allowed:
example.tswith
{ "test": "for" }No problemsimport { expect, it } from "vitest";
it.each([1, 2, 3])("accepts %i", (value) => {
expect(value).toBeGreaterThan(0);
});