jest/prefer-called-with Style
它的作用
建议使用 toBeCalledWith() 或 toHaveBeenCalledWith()
为什么这不好?
在测试函数调用时,通常更有价值的是同时断言: 函数已被调用,以及它被调用时传入了哪些参数。 使用 toBeCalled() 或 toHaveBeenCalled() 只能验证函数 被调用了,但不能验证参数,可能会漏掉 函数以错误参数被调用的 bug。
示例
以下是此规则的不正确代码示例:
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();如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["jest"],
"rules": {
"jest/prefer-called-with": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/prefer-called-with": "error",
},
});bash
oxlint --deny jest/prefer-called-with --jest-plugin版本
此规则于 v0.2.5 中添加。
