Skip to content
Latchkey

Bun "expect(received).toBe(expected)" assertion failure in CI

A bun:test matcher reported that the received value does not equal the expected value. The failure block shows both sides and a diff so you can see exactly what differed.

What this error means

bun test prints an "(fail)" block with "expect(received).toBe(expected)" (or toEqual, toContain) and a colored diff of the two values.

Terminal
(fail) format > formats currency
error: expect(received).toBe(expected)

  Expected: "$1,000.00"
  Received: "$1000"

Common causes

The code produces a different value than asserted

A logic change or an environment difference (locale, rounding) makes the received value diverge from the expectation.

A strict matcher on a structurally-equal value

Using toBe (reference equality) where toEqual (deep equality) is meant fails on two equal-but-distinct objects.

How to fix it

Compare expected and received, then fix one side

  1. Read the Expected and Received values in the fail block.
  2. Decide whether the code or the expectation is wrong.
  3. Fix the logic, or update the expectation if the new value is correct.
src/format.test.ts
import { expect, test } from "bun:test";
test("formats currency", () => {
  expect(formatCurrency(1000)).toBe("$1,000.00");
});

Use the right matcher for the value

Use toEqual for deep structural comparison of objects and arrays instead of toBe reference equality.

src/format.test.ts
expect(result).toEqual({ total: 1000, currency: "USD" });

How to prevent it

  • Pick toEqual for structural comparisons, toBe for primitives/refs.
  • Make formatting deterministic (fixed locale/timezone) in tests.
  • Assert on stable, normalized values.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →