jest/prefer-to-be Style
作用
建议对原始字面量使用 toBe 匹配器,并对 null、undefined 和 NaN 使用特定的匹配器。
为什么这不好?
当对数字和字符串等原始字面量进行断言时,所有相等性匹配器的行为都相同,但在代码中的读法略有不同。
该规则建议在这些情况下使用 toBe 匹配器,因为它形成的句子最符合语法习惯。对于 null、undefined 和 NaN,该规则建议使用它们各自特定的 toBe 匹配器,因为它们也能提供更好的错误信息。
示例
以下是此规则的错误代码示例:
javascript
expect(value).not.toEqual(5);
expect(getMessage()).toStrictEqual("hello world");
expect(loadMessage()).resolves.toEqual("hello world");以下是此规则的正确代码示例:
javascript
expect(value).not.toBe(5);
expect(getMessage()).toBe("hello world");
expect(loadMessage()).resolves.toBe("hello world");
expect(didError).not.toBe(true);
expect(catchError()).toStrictEqual({ message: "oh noes!" });如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["jest"],
"rules": {
"jest/prefer-to-be": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/prefer-to-be": "error",
},
});bash
oxlint --deny jest/prefer-to-be --jest-plugin版本
此规则在 v0.2.14 中加入。
