# bun:test/prefer-hooks-in-order

> Require hooks to be written in the order they run

`bun test` runs hooks in a fixed order: `beforeAll`, `beforeEach`, `afterEach`, `afterAll`. If a file lists them in another order, you read the teardown before the setup it undoes.

Each `describe` block is checked on its own. Only hooks written one after another are compared. Any other call between two hooks, such as a test, starts a new group.

{/* 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-hooks-in-order" = "error" # or "warn"
```

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

## Examples

Reported:

{/* bad */}
```ts
import { afterEach, beforeEach } from "bun:test";

afterEach(() => {
  db.rollback();
});

beforeEach(() => {
  db.begin();
});
```

Not reported:

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

beforeEach(() => {
  db.begin();
});

afterEach(() => {
  db.rollback();
});
```

The test between these two hooks starts a new group, so this isn't reported. `bun:test/prefer-hooks-on-top` reports it instead.

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

afterEach(() => {
  db.rollback();
});

test("saves a user", () => {
  db.save(user);
});

beforeEach(() => {
  db.begin();
});
```
