vitest/prefer-to-be-falsy 风格
它做了什么
当使用 expect 或 expectTypeOf 时,如果出现 toBe(false),此规则会发出警告。启用 --fix 后,将被替换为 toBeFalsy()。
为什么这是不好的?
使用 toBe(false) 表达力较弱,且可能无法涵盖其他假值(如 0、null、undefined)。而 toBeFalsy() 能够更全面地检查任意假值,从而提升测试的健壮性。
示例
此规则的错误代码示例:
javascript
expect(foo).toBe(false);
expectTypeOf(foo).toBe(false);此规则的正确代码示例:
javascript
expect(foo).toBeFalsy();
expectTypeOf(foo).toBeFalsy();如何使用
要通过配置文件或 CLI 启用此规则,可以使用:
json
{
"plugins": ["vitest"],
"rules": {
"vitest/prefer-to-be-falsy": "error"
}
}bash
oxlint --deny vitest/prefer-to-be-falsy --vitest-plugin