Skip to content
← Back to rules

vitest/prefer-strict-equal Style

🛠️ 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" });

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/prefer-strict-equal": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vitest"],
  rules: {
    "vitest/prefer-strict-equal": "error",
  },
});
bash
oxlint --deny vitest/prefer-strict-equal --vitest-plugin

版本

此规则于 v0.2.13 中添加。

参考资料