# prefer-hooks-in-order

Rule `vitest/prefer-hooks-in-order`. Enforce having hooks in consistent order

- Source: vitest
- Technologies: Testing
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 758.7K weekly downloads of presets that enable it
- Trend: +49.9% last quarter
- Rank: #1571 by reach, #788 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `vitest/prefer-hooks-in-order` | no | no | package `@vitest/eslint-plugin` | [docs](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/prefer-hooks-in-order.md) |
| Oxlint | `vitest/prefer-hooks-in-order` | no | no | plugin `vitest`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/vitest/prefer-hooks-in-order.html) |
| Biome | `style/useTestHooksInOrder` | no | no | group style, since 2.4.15 | [docs](https://biomejs.dev/linter/rules/use-test-hooks-in-order/) |

## Code examples

### Incorrect: the rule reports this

```js
describe('foo', () => {
  beforeEach(() => {});
  beforeAll(() => {});
});
```

```js
describe('foo', () => {
  afterEach(() => {});
  afterAll(() => {});
  beforeAll(() => {});
});
```

### Correct: the rule accepts this

```js
describe('foo', () => {
  beforeAll(() => {});
  beforeEach(() => {});
  afterEach(() => {});
  afterAll(() => {});
});
```

```js
// Hooks separated by test cases are treated independently.
describe('foo', () => {
  beforeEach(() => {});
  it('a test', () => {});
  afterAll(() => {});
});
```

Examples from the Biome documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import vitest from "@vitest/eslint-plugin";

export default [
  {
    plugins: { vitest },
    rules: {
      "vitest/prefer-hooks-in-order": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "plugins": [
    "vitest"
  ],
  "rules": {
    "vitest/prefer-hooks-in-order": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "style": {
        "useTestHooksInOrder": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Anthony Fu's config](https://lintradar.dev/presets/antfu.md) | ESLint | `@antfu/eslint-config` | 294 | 758.7K | +49.9% |

## Related rules

- [jest/prefer-hooks-in-order](https://lintradar.dev/rules/jest/prefer-hooks-in-order.md): Prefer having hooks in a consistent order
- [vitest/consistent-test-it](https://lintradar.dev/rules/vitest/consistent-test-it.md): Enforce using test or it but not both
- [vitest/prefer-lowercase-title](https://lintradar.dev/rules/vitest/prefer-lowercase-title.md): Enforce lowercase titles
- [vitest/max-nested-describe](https://lintradar.dev/rules/vitest/max-nested-describe.md): Require describe block to be less than set max value or default value
- [vitest/no-duplicate-hooks](https://lintradar.dev/rules/vitest/no-duplicate-hooks.md): Disallow duplicate hooks and teardown hooks
- [vitest/no-hooks](https://lintradar.dev/rules/vitest/no-hooks.md): Disallow setup and teardown hooks

---

Web page: https://lintradar.dev/rules/vitest/prefer-hooks-in-order. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
