vitest/prefer-each
Require test.each and describe.each instead of a loop around tests
test.each and describe.each take the list of cases as an argument and fill in the title for each one. A for loop around test does the same thing with more code, and you have to build every title by hand.
The rule reports for, for...of and for...in loops that contain a test, a describe or a hook such as beforeEach. A loop inside a test is fine. while loops and cases.forEach(...) around a test aren't reported.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"vitest/prefer-each" = "error" # or "warn"Examples#
example.ts1 problem
import { expect, test } from "vitest";
const cases = [
[1, 2],
[2, 4],
];
for (const [input, expected] of cases) Enforce using each rather than manual loops{
test(`doubles ${input}`, () => {
expect(input * 2).toBe(expected);
});
}example.tsNo problems
import { expect, test } from "vitest";
test.each([
[1, 2],
[2, 4],
])("doubles %i", (input, expected) => {
expect(input * 2).toBe(expected);
});
test("every price is positive", () => {
for (const price of [5, 10]) {
expect(price).toBeGreaterThan(0);
}
});