vitest/prefer-called-exactly-once-with Style
作用
它会检查目标是否同时使用了 toHaveBeenCalledOnce 和 toHaveBeenCalledWith 来断言,而不是使用 toHaveBeenCalledExactlyOnceWith。
为什么这不好?
读者必须从这两个期望中推断出 spy 函数只被调用了一次,并且使用了特定参数。
示例
以下是此规则的错误代码示例:
js
test("foo", () => {
const mock = vi.fn();
mock("foo");
expect(mock).toHaveBeenCalledOnce();
expect(mock).toHaveBeenCalledWith("foo");
});以下是此规则的正确代码示例:
js
test("foo", () => {
const mock = vi.fn();
mock("foo");
expect(mock).toHaveBeenCalledExactlyOnceWith("foo");
});如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/prefer-called-exactly-once-with": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/prefer-called-exactly-once-with": "error",
},
});bash
oxlint --deny vitest/prefer-called-exactly-once-with --vitest-plugin版本
此规则已在 v1.58.0 中添加。
