vitest/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": ["vitest"],
"rules": {
"vitest/prefer-to-be": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/prefer-to-be": "error",
},
});bash
oxlint --deny vitest/prefer-to-be --vitest-plugin版本
此规则在 v0.2.14 中添加。
