Skip to content
← Back to rules

jest/prefer-called-with スタイル

An auto-fix is available for this rule.

何をしますか

toBeCalledWith() もしくは toHaveBeenCalledWith() の使用を推奨します

なぜ悪いですか

関数の呼び出しをテストする際、関数が呼び出されたことだけでなく、どのような引数で呼び出されたかも確認することがしばしば重要です。
toBeCalled()toHaveBeenCalled() では、関数が呼び出されたかどうかだけを検証するため、引数の検証は行われず、誤ったパラメータで関数が呼び出されている場合にバグを見逃す可能性があります。

このルールに違反する不正なコードの例:

javascript
expect(someFunction).toBeCalled();
expect(someFunction).toHaveBeenCalled();

このルールに準拠する正しいコードの例:

javascript
expect(noArgsFunction).toBeCalledWith();
expect(roughArgsFunction).toBeCalledWith(expect.anything(), expect.any(Date));
expect(anyArgsFunction).toBeCalledTimes(1);
expect(uncalledFunction).not.toBeCalled();

このルールは eslint-plugin-vitest と互換性があります。
これを使用するには、.oxlintrc.json に以下の設定を追加してください:

json
{
  "rules": {
    "vitest/prefer-called-with": "error"
  }
}

使用方法

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

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-called-with": "error"
  }
}
bash
oxlint --deny jest/prefer-called-with --jest-plugin

参照