# bun:test/prefer-to-have-length

> Require `toHaveLength()` instead of comparing the `length` property

When `expect(users.length).toBe(2)` fails, the message shows two bare numbers. `expect(users).toHaveLength(2)` labels them as the expected and the received length. If the value has no `length` property, it says so instead of comparing `undefined` with `2`.

The rule reports `.length` inside `expect()` when the matcher is `toBe()`, `toEqual()`, or `toStrictEqual()`.

`--fix` rewrites the assertion.

{/* 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/prefer-to-have-length" = "error" # or "warn"
```

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

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
import { expect, test } from "bun:test";

test("finds the active users", () => {
  const users = findUsers({ active: true });
  expect(users.length).toBe(2);
  expect(users[0].roles.length).not.toEqual(0);
});
```

Not reported:

{/* good */}
```ts
import { expect, test } from "bun:test";

test("finds the active users", () => {
  const users = findUsers({ active: true });
  expect(users).toHaveLength(2);
  expect(users[0].roles).not.toHaveLength(0);
  expect(users.length).toBeGreaterThan(1);
});
```
