跳至內容

jest/consistent-test-it 風格

🛠️ 此規則提供自動修正。

功能

Jest 允許你選擇如何定義你的測試,使用 ittest 關鍵字,每個都有多種變化。

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

範例

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

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

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

選項

此規則可配置如下:

json5
{
  type: "object",
  properties: {
    fn: {
      enum: ["it", "test"],
    },
    withinDescribe: {
      enum: ["it", "test"],
    },
  },
  additionalProperties: false,
}
fn

決定使用 testit

withinDescribe

決定在 describe 範圍內使用 testit

此規則與 eslint-plugin-vitest 相容,要使用它,請將以下設定新增至您的 .eslintrc.json

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


## References
- [Rule Source](https://github.com/oxc-project/oxc/blob/main/crates/oxc_linter/src/rules/jest/consistent_test_it.rs)

以 MIT 授權釋出。