Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

vitest/prefer-comparison-matcher

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

expect(price < 100).toBe(true) compares first and only hands expect 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 rewrites the assertion.

Configure

Off by default. To turn it on:

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

Examples

Reported:

import { expect, test } from "vitest";

test("applies the discount", () => {
  const price = discounted(100);
  expect(price < 100).toBe(true);
  expect(price < 0).toBe(false);
});

Not reported:

import { expect, test } from "vitest";

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 the rule skips a comparison with a string literal or a template literal:

import { expect, test } from "vitest";

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, and --fix rewrites the assertion anyway. This test is correct as written:

import { expect, test } from "vitest";

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