Skip to content
← Back to rules

jest/prefer-strict-equal 风格

An auto-fix is available for this rule.

它的作用

如果使用 toEqual() 来断言相等性,此规则会触发警告。

为什么这是不好的?

toEqual() 匹配器执行深度相等性检查,但会忽略对象和数组中的 undefined 值。这可能导致误报,即测试通过了本应失败的情况。toStrictEqual() 通过检查 undefined 值提供了更准确的比较。

示例

此规则的 错误 代码示例:

javascript
expect({ a: "a", b: undefined }).toEqual({ a: "a" });

此规则的 正确 代码示例:

javascript
expect({ a: "a", b: undefined }).toStrictEqual({ a: "a" });

此规则与 eslint-plugin-vitest 兼容。要使用它,请将以下配置添加到您的 .oxlintrc.json 文件中:

json
{
  "rules": {
    "vitest/prefer-strict-equal": "error"
  }
}

如何使用

要通过配置文件或命令行 启用 此规则,可以使用:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-strict-equal": "error"
  }
}
bash
oxlint --deny jest/prefer-strict-equal --jest-plugin

参考资料