Skip to content
← Back to rules

jest/consistent-test-it 风格

An auto-fix is available for this rule.

它的作用

Jest 允许你选择使用 ittest 关键字来定义测试,每种方式都有多种变体:

  • it: it, xit, fit, it.only, it.skip
  • test: test, xtest, test.only, test.skip

为什么这是个问题?

在测试套件中保持一致性是一种良好的实践,以确保所有测试都采用相同的书写方式。

示例

javascript
/* jest/consistent-test-it: ["error", {"fn": "test"}] */
test("foo"); // 有效
test.only("foo"); // 有效

it("foo"); // 无效
it.only("foo"); // 无效
javascript
/* jest/consistent-test-it: ["error", {"fn": "it"}] */
it("foo"); // 有效
it.only("foo"); // 有效
test("foo"); // 无效
test.only("foo"); // 无效
javascript
/* jest/consistent-test-it: ["error", {"fn": "it", "withinDescribe": "test"}] */
it("foo"); // 有效
describe("foo", function () {
  test("bar"); // 有效
});

test("foo"); // 无效
describe("foo", function () {
  it("bar"); // 无效
});

此规则与 eslint-plugin-vitest 兼容,要使用它,请将以下配置添加到你的 .oxlintrc.json 中:

json
{
  "rules": {
    "vitest/consistent-test-it": "error"
  }
}

配置

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

fn

type: "it" | "test"

default: "test"

决定使用 test 还是 it

withinDescribe

type: "it" | "test"

default: "it"

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

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/consistent-test-it": "error"
  }
}
bash
oxlint --deny jest/consistent-test-it --jest-plugin

参考资料