vitest/prefer-to-be-falsy
Require toBeFalsy() instead of toBe(false)
has suggestions
toBe(false) only passes for false itself. If the code returns undefined, null, or 0 instead, the test fails, even though if (!value) treats them all the same. toBeFalsy() passes for any falsy value.
toEqual(false) and toStrictEqual(false) are reported too, with or without .not.
--fix-suggestions replaces the matcher with toBeFalsy(). --fix doesn't, because the new matcher accepts more values.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"vitest/prefer-to-be-falsy" = "error" # or "warn"Examples#
example.ts2 problems
import { expect, test } from "vitest";
test("a new cart has no discount", () => {
const cart = createCart();
expect(cart.hasDiscount).toBe(false)Use toBeFalsy instead.;
expect(cart.isOpen).not.toEqual(false)Use toBeFalsy instead.;
});example.tsNo problems
import { expect, test } from "vitest";
test("a new cart has no discount", () => {
const cart = createCart();
expect(cart.hasDiscount).toBeFalsy();
expect(cart.isOpen).not.toBeFalsy();
});When not to use it#
toBe(false) is the stricter check. If returning undefined or 0 instead of false would be a bug, keep it. vitest/prefer-strict-boolean-matchers is the opposite rule.