Skip to content
← Back to rules

vitest/prefer-to-be-falsy Style

💡 A suggestion is available for this rule.

作用 ​

当 expect 或 expectTypeOf 使用 toBe(false) 时,此规则会发出警告。 使用 --fix-suggestions 时,它会被替换为 toBeFalsy()。

为什么这不好? ​

测试假值时,toBeFalsy() 能直接表达这一意图。 与 toBe(false) 不同,它还接受 0、null 和 undefined 等非布尔假值。由于替换会改变断言通过的值,因此它被作为建议提供。

示例 ​

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

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 中添加的。

参考资料 ​