jest/prefer-equality-matcher Style
它的作用
Jest 内置了用于判断相等性的 matcher,可以让测试代码更易读; 当断言失败时,错误信息也更清晰。
为什么这很糟糕?
使用像 toBe(true) 这样的通用 matcher 来测试相等表达式,会让测试更难阅读和理解。 当测试失败时,错误信息也不够有帮助,因为它们不会展示实际值是什么。 使用更具体的相等 matcher 可以让测试意图更明确,并提供更好的调试信息。
示例
以下是此规则的错误代码示例:
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": ["jest"],
"rules": {
"jest/prefer-equality-matcher": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/prefer-equality-matcher": "error",
},
});bash
oxlint --deny jest/prefer-equality-matcher --jest-plugin版本
此规则是在 v0.2.9 中添加的。
