Skip to content
← Back to rules

vitest/prefer-strict-boolean-matchers Style

🛠️ An auto-fix is available for this rule.

作用

强制使用 toBe(true)toBe(false),而不是会将类型强制转换为布尔值的匹配器。

为什么这不好?

Truthy/falsy 匹配器会将值强制转换为布尔值,并可能掩盖类型错误。 严格的布尔断言能明确表达意图,并避免意外的类型转换。

示例

以下是此规则的错误代码示例:

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

以下是此规则的正确代码示例:

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

如何使用

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

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

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

版本

此规则于 v1.57.0 中添加。

参考资料