bun:test/prefer-comparison-matcher

Require matchers such as toBeGreaterThan() instead of comparing inside expect()

has suggestions

expect(price < 100).toBe(true) compares first and hands expect only the result. When it fails, the message says that false isn't true, and you don't see the price. expect(price).toBeLessThan(100) prints both numbers.

The rule reports >, >=, < and <= inside expect() when the result is checked against true or false with toBe(), toEqual() or toStrictEqual().

--fix-suggestions rewrites the assertion. --fix doesn't, because the comparison matchers throw on anything that isn't a number or a bigint.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun:test/prefer-comparison-matcher" = "error" # or "warn"

Examples#

example.ts2 problems
import { expect, test } from "bun:test";

test("applies the discount", () => {
  const price = discounted(100);
  expect(price < 100).toBe(true);
  expect(price < 0).toBe(false);
});
example.tsNo problems
import { expect, test } from "bun:test";

test("applies the discount", () => {
  const price = discounted(100);
  expect(price).toBeLessThan(100);
  expect(price).toBeGreaterThanOrEqual(0);
});

The comparison matchers only take numbers and bigints, so a comparison with a string literal or a template literal is left alone:

example.tsNo problems
import { expect, test } from "bun:test";

test("sorts after the first entry", () => {
  expect(lastName() > "Adams").toBe(true);
});

When not to use it#

The rule doesn't look at types, so it also reports comparisons of dates, or of strings held in variables. The comparison matchers throw on those values. This test is correct as written:

example.ts1 problem
import { expect, test } from "bun:test";

test("renewing extends the subscription", () => {
  expect(renewed.expiresAt > current.expiresAt).toBe(true);
});