jest/prefer-to-be 风格
它做了什么
推荐对原始字面量使用 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!" });该规则与 eslint-plugin-vitest 兼容。要使用它,请将以下配置添加到您的 .oxlintrc.json 中:
json
{
"rules": {
"vitest/prefer-to-be": "error"
}
}如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"plugins": ["jest"],
"rules": {
"jest/prefer-to-be": "error"
}
}bash
oxlint --deny jest/prefer-to-be --jest-plugin