vitest/prefer-equality-matcher 样式
它的作用
Jest 为期望相等性提供了内置的匹配器,这使测试更易读, 如果断言失败,错误信息也更清晰。
为什么这不好?
使用像 toBe(true) 这样的通用匹配器来测试相等表达式, 会让测试更难阅读和理解。当测试失败时,错误信息也不够有帮助, 因为它们不会显示实际值是什么。使用特定的相等匹配器可以提供更清晰的测试意图和 更好的调试信息。
示例
以下是此规则的错误代码示例:
javascript
expect(x === 5).toBe(true);
expect(name === "Carl").not.toEqual(true);
expect(myObj !== thatObj).toStrictEqual(true);以下是此规则的正确代码示例:
javascript
expect(x).toBe(5);
expect(name).not.toEqual("Carl");
expect(myObj).toStrictEqual(thatObj);如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/prefer-equality-matcher": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/prefer-equality-matcher": "error",
},
});bash
oxlint --deny vitest/prefer-equality-matcher --vitest-plugin版本
此规则是在 v0.2.9 中添加的。
