bun:test/max-expects

Enforce a maximum number of expect() calls in a test

A test with many assertions usually checks several things at once. The first expect that fails stops the test and hides any failures after it, and the test's name no longer tells you what broke.

By default a test can have 5 expect() calls. Each call after that is reported.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun:test/max-expects" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"bun:test/max-expects" = ["error", {
  max = 5,
}]

Examples#

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

test("creates a user", () => {
  const user = createUser({ name: "Ada", email: "ada@example.com" });
  expect(user.id).toBeGreaterThan(0);
  expect(user.name).toBe("Ada");
  expect(user.email).toBe("ada@example.com");
  expect(user.role).toBe("member");
  expect(user.active).toBe(true);
  expect(user.createdAt).toBeInstanceOf(Date);
});
example.tsNo problems
import { expect, test } from "bun:test";

test("keeps the name and the email", () => {
  const user = createUser({ name: "Ada", email: "ada@example.com" });
  expect(user).toMatchObject({ name: "Ada", email: "ada@example.com" });
});

test("fills in the defaults", () => {
  const user = createUser({ name: "Ada", email: "ada@example.com" });
  expect(user.id).toBeGreaterThan(0);
  expect(user.role).toBe("member");
  expect(user.active).toBe(true);
  expect(user.createdAt).toBeInstanceOf(Date);
});

Options#

OptionDefaultWhat it does
max5The number of expect() calls allowed.
bunfig.toml
[lint.rules]
"bun:test/max-expects" = ["error", { max = 2 }]

With max = 2, the third call is reported:

example.tswith { "max": 2 }1 problem
import { expect, test } from "bun:test";

test("parses a URL", () => {
  const url = parse("https://example.com:8080/docs");
  expect(url.host).toBe("example.com");
  expect(url.port).toBe(8080);
  expect(url.path).toBe("/docs");
});

An expect in a loop, an if or a callback counts toward the test around it:

example.tswith { "max": 2 }1 problem
import { expect, test } from "bun:test";

test("lists active users", () => {
  const users = listUsers({ active: true });
  expect(users).toHaveLength(2);
  expect(users[0].name).toBe("Ada");
  for (const user of users) {
    expect(user.active).toBe(true);
  }
});

A helper function gets its own count. expect.assertions() and expect.hasAssertions() aren't counted.