Skip to content
← Back to rules

vitest/consistent-each-for Correctness

它的作用

此规则强制用于创建参数化测试的方法保持一致。 此配置会影响不同的测试函数类型(testitdescribesuite)。

为什么这不好?

如果没有一种一致的方式来创建参数化测试,我们就不得不依赖开发者记住: .for 会将这些值作为不同的参数展开,而 .each 会将数组作为单个参数传递。

示例

以下是此规则的错误代码示例:

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", () => {});
});

以下是此规则的正确代码示例:

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", () => {});
});

配置

此规则接受一个包含以下属性的配置对象:

describe

type: "for" | "each"

用于为 describe 块创建参数化测试的首选方法。

it

type: "for" | "each"

用于为 it 块创建参数化测试的首选方法。

suite

type: "for" | "each"

用于为 suite 块创建参数化测试的首选方法。

test

type: "for" | "each"

用于为 test 块创建参数化测试的首选方法。

如何使用

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

此规则在 v1.39.0 中添加。

References