Skip to content
← Back to rules

jest/consistent-test-it スタイル

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

何を検査するか

Jest では、テストの定義に it または test キーワードのどちらを使用するかを選択できます。それぞれに対して複数のバリエーションが存在します:

  • 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"

testit のどちらを使用するかを決定します。

withinDescribe

type: "it" | "test"

default: "it"

describe スコープ内での testit のどちらを使用するかを決定します。
fn だけが指定された場合、これは fn の値にデフォルトされます。

使用方法

このルールを有効化するには、設定ファイルまたは CLI で以下のように使用できます:

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

参照