Skip to content
← Back to rules

vitest/consistent-test-it Style

🛠️ An auto-fix is available for this rule.

它的作用

Vitest 允许你选择如何定义测试,使用 ittest 关键字,并且每种都有多种变体:

  • it: itit.onlyit.skipit.concurrentit.each
  • test: testtest.onlytest.skiptest.concurrenttest.each

为什么这不好?

在测试套件中保持一致是一种良好的实践,这样所有测试都可以用相同的方式编写。

Configuration

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

fn

type: "it" | "test"

default: "test"

决定使用 test 还是 it

Examples of incorrect code for { "fn": "test" }:

javascript
it("foo");
it.only("foo");

Examples of correct code for { "fn": "test" }:

javascript
test("foo");
test.only("foo");

Examples of incorrect code for { "fn": "it" }:

javascript
test("foo");
test.only("foo");

Examples of correct code for { "fn": "it" }:

javascript
it("foo");
it.only("foo");

withinDescribe

type: "it" | "test"

default: "it"

决定在 describe 作用域内使用 test 还是 it。 如果只提供了 fn,此项将默认使用 fn 的值。

Examples of incorrect code for { "withinDescribe": "test" }:

javascript
describe("foo", function () {
  it("bar");
});

Examples of correct code for { "withinDescribe": "test" }:

javascript
describe("foo", function () {
  test("bar");
});

How to use

To enable this rule using the config file or in the CLI, you can use:

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

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

版本

此规则在 v0.5.3 中添加。

参考资料