jest/prefer-strict-equal Style
作用
如果使用 toEqual() 来断言相等,此规则会触发警告。
为什么这不好?
toEqual() 匹配器会执行深度相等检查,但会忽略对象和数组中的 undefined 值。这可能会导致误报,即测试本应失败却通过了。toStrictEqual() 会通过检查 undefined 值提供更准确的比较。
示例
以下是此规则的错误代码示例:
javascript
expect({ a: "a", b: undefined }).toEqual({ a: "a" });以下是此规则的正确代码示例:
javascript
expect({ a: "a", b: undefined }).toStrictEqual({ a: "a" });如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["jest"],
"rules": {
"jest/prefer-strict-equal": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/prefer-strict-equal": "error",
},
});bash
oxlint --deny jest/prefer-strict-equal --jest-plugin版本
此规则于 v0.2.13 中添加。
