# 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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

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

Every option, set to its default:

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 1} */}
```ts
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);
});
```

Not reported:

{/* good */}
```ts
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

| Option | Default | What it does |
| --- | --- | --- |
| `max` | `5` | The number of `expect()` calls allowed. |

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

With `max = 2`, the third call is reported:

{/* bad {"options": [{"max": 2}], "count": 1} */}
```ts
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:

{/* bad {"options": [{"max": 2}], "count": 1} */}
```ts
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.
