Skip to content
← Back to rules

vitest/prefer-to-be-falsy Style

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

作用

当在 expectexpectTypeOf 中使用 toBe(false) 时,此规则会发出警告。 使用 --fix 时,它会被替换为 toBeFalsy()

为什么这不好?

使用 toBe(false) 的表达性较差,而且可能无法涵盖其他假值, 例如 0nullundefinedtoBeFalsy() 能更全面地检查任何假值, 从而提高测试的健壮性。

示例

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

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

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

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

如何使用

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

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

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

版本

此规则是在 v0.7.1 中添加的。

参考资料