# prefer-hooks-on-top

Rule `vitest/prefer-hooks-on-top`. Enforce having hooks before any test cases

- Source: vitest
- Technologies: Testing
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #1690 by reach

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
describe('foo', () => {
  it('does something', () => {});
  beforeEach(() => {});
});
```

### Correct: the rule accepts this

```js
describe('foo', () => {
  beforeEach(() => {});
  it('does something', () => {});
});
```

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-on-top": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

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

### Biome: `biome.json`

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

## Related rules

- [jest/prefer-hooks-on-top](https://lintradar.dev/rules/jest/prefer-hooks-on-top.md): Suggest having hooks before any test cases
- [vitest/prefer-equality-matcher](https://lintradar.dev/rules/vitest/prefer-equality-matcher.md): Enforce using the built-in equality matchers
- [vitest/prefer-importing-vitest-globals](https://lintradar.dev/rules/vitest/prefer-importing-vitest-globals.md): Enforce importing Vitest globals
- [vitest/prefer-describe-function-title](https://lintradar.dev/rules/vitest/prefer-describe-function-title.md): Enforce using a function as a describe title over an equivalent string
- [vitest/prefer-spy-on](https://lintradar.dev/rules/vitest/prefer-spy-on.md): Enforce using `vi.spyOn`
- [vitest/prefer-called-with](https://lintradar.dev/rules/vitest/prefer-called-with.md): Enforce using `toBeCalledWith()` or `toHaveBeenCalledWith()`

---

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