Skip to content
← Back to rules

vitest/prefer-to-be-truthy 风格

An auto-fix is available for this rule.

它的作用

当使用 expectexpectTypeOf 时,若出现 toBe(true),此规则会发出警告。启用 --fix 后,将自动替换为 toBeTruthy()

为什么这是不好的?

使用 toBe(true) 灵活性较差,可能无法涵盖其他真值(如非空字符串或对象)。而 toBeTruthy() 会检查任意真值,使测试更具全面性和健壮性。

示例

此规则的 错误 代码示例:

javascript
expect(foo).toBe(true);
expectTypeOf(foo).toBe(true);

此规则的 正确 代码示例:

javascript
expect(foo).toBeTruthy();
expectTypeOf(foo).toBeTruthy();

如何使用

通过配置文件或 CLI 启用此规则,可以使用以下方式:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/prefer-to-be-truthy": "error"
  }
}
bash
oxlint --deny vitest/prefer-to-be-truthy --vitest-plugin

参考资料