# prefer-hooks-on-top

Rule `jest/prefer-hooks-on-top`. Suggest having hooks before any test cases

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

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `jest/prefer-hooks-on-top` | no | no | package `eslint-plugin-jest` | [docs](https://github.com/jest-community/eslint-plugin-jest/blob/v29.17.2/docs/rules/prefer-hooks-on-top.md) |
| Oxlint | `jest/prefer-hooks-on-top` | no | no | plugin `jest`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/jest/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', () => {
  beforeEach(() => {
    seedMyDatabase();
  });

  it('accepts this input', () => {
    // ...
  });

  beforeAll(() => {
    createMyDatabase();
  });

  it('returns that value', () => {
    // ...
  });

  describe('when the database has specific values', () => {
    const specificValue = '...';

    beforeEach(() => {
      seedMyDatabase(specificValue);
    });

    it('accepts that input', () => {
      // ...
    });

    it('throws an error', () => {
      // ...
    });

// …
```

### Correct: the rule accepts this

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

  beforeEach(() => {
    seedMyDatabase();
  });

  afterAll(() => {
    clearMyDatabase();
  });

  it('accepts this input', () => {
    // ...
  });

  it('returns that value', () => {
    // ...
  });

  describe('when the database has specific values', () => {
    const specificValue = '...';

    beforeEach(() => {
      seedMyDatabase(specificValue);
    });

    beforeEach(() => {
      mockLogger();
    });

// …
```

Examples from the ESLint documentation.

## Enable it

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

```js
import jest from "eslint-plugin-jest";

export default [
  {
    plugins: { jest },
    rules: {
      "jest/prefer-hooks-on-top": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

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

### Biome: `biome.json`

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

## Related rules

- [vitest/prefer-hooks-on-top](https://lintradar.dev/rules/vitest/prefer-hooks-on-top.md): Enforce having hooks before any test cases
- [jest/prefer-hooks-in-order](https://lintradar.dev/rules/jest/prefer-hooks-in-order.md): Prefer having hooks in a consistent order
- [jest/prefer-importing-jest-globals](https://lintradar.dev/rules/jest/prefer-importing-jest-globals.md): Prefer importing Jest globals
- [jest/prefer-equality-matcher](https://lintradar.dev/rules/jest/prefer-equality-matcher.md): Suggest using the built-in equality matchers
- [jest/prefer-lowercase-title](https://lintradar.dev/rules/jest/prefer-lowercase-title.md): Enforce lowercase test names
- [jest/prefer-called-with](https://lintradar.dev/rules/jest/prefer-called-with.md): Suggest using `toHaveBeenCalledWith()`

---

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