Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

bun:test/prefer-strict-equal

Require toStrictEqual() instead of toEqual()

toEqual() skips properties that are undefined, treats a hole in an array like undefined, and doesn't compare the classes of the two objects. So expect({ name: "Ada", email: undefined }).toEqual({ name: "Ada" }) passes. toStrictEqual() checks all three, and that test fails.

--fix-suggestions renames the matcher. --fix doesn't, because a test that passes with toEqual() can fail with toStrictEqual().

Configure

Off by default. To turn it on:

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

Examples

Reported:

import { expect, test } from "bun:test";

test("creates a user", () => {
  expect(createUser("Ada")).toEqual({ name: "Ada" });
});

Not reported:

import { expect, test } from "bun:test";

test("creates a user", () => {
  expect(createUser("Ada")).toStrictEqual({ name: "Ada" });
});

When not to use it

If your code builds objects with optional properties set to undefined, or you compare class instances with plain objects on purpose, toEqual() is the matcher you want.