vitest/consistent-each-for Correctness
What it does
This rule ensure consistency on which method used to create parameterized test. This configuration affects to different test function types (test, it, describe, suite).
Why is this bad?
Not having a consistent way to create parametrized tests, we rely on the developer to remember that .for spread the values as different arguments and .each pass the array as an unique argument.
Examples
Examples of incorrect code for this rule:
// { test: 'for' }
test.each([[1, 1, 2]])("test", (a, b, expected) => {
expect(a + b).toBe(expected);
});
// { describe: 'for' }
describe.each([[1], [2]])("suite %s", (n) => {
test("test", () => {});
});Examples of correct code for this rule:
// { test: 'for' }
test.for([[1, 1, 2]])("test", ([a, b, expected]) => {
expect(a + b).toBe(expected);
});
// { describe: 'for' }
describe.for([[1], [2]])("suite %s", ([n]) => {
test("test", () => {});
});Configuration
This rule accepts a configuration object with the following properties:
describe
type: "for" | "each"
Preferred method to create parameterized tests for describe blocks.
it
type: "for" | "each"
Preferred method to create parameterized tests for it blocks.
suite
type: "for" | "each"
Preferred method to create parameterized tests for suite blocks.
test
type: "for" | "each"
Preferred method to create parameterized tests for test blocks.
How to use
To enable this rule using the config file or in the CLI, you can use:
{
"plugins": ["vitest"],
"rules": {
"vitest/consistent-each-for": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/consistent-each-for": "error",
},
});oxlint --deny vitest/consistent-each-for --vitest-plugin