Skip to content
← Back to rules

vitest/consistent-each-for Correctness

What it does

This rule enforces consistency in which method is used to create parameterized tests. This configuration affects different test function types (test, it, describe, suite).

Why is this bad?

Without a consistent way to create parameterized tests, we rely on the developer to remember that .for spreads the values as different arguments while .each passes the array as a single argument.

Examples

Examples of incorrect code for this rule:

js
// { 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:

js
// { 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:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/consistent-each-for": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vitest"],
  rules: {
    "vitest/consistent-each-for": "error",
  },
});
bash
oxlint --deny vitest/consistent-each-for --vitest-plugin

Version

This rule was added in v1.39.0.

References