Skip to content

useTestHooksOnTop

biome.json
{
"linter": {
"rules": {
"nursery": {
"useTestHooksOnTop": "error"
}
}
}
}

Enforce that lifecycle hooks appear before any test cases in the same block.

Placing beforeEach, beforeAll, afterEach, and afterAll hooks after test cases (it, test) makes the setup and teardown harder to spot at a glance and can be a source of confusion for readers of the test suite.

describe('foo', () => {
it('does something', () => {});
beforeEach(() => {});
});
code-block.js:3:3 lint/nursery/useTestHooksOnTop ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Lifecycle hook beforeEach appears after a test case.

1 │ describe(‘foo’, () => {
2 │ it(‘does something’, () => {});
> 3 │ beforeEach(() => {});
^^^^^^^^^^^^^^^^^^^^
4 │ });
5 │

Placing the hook after this test case makes it harder to spot the setup and teardown for these tests at a glance.

1 │ describe(‘foo’, () => {
> 2 │ it(‘does something’, () => {});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ beforeEach(() => {});
4 │ });

Move the hook above all test cases in the same block for better readability.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

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