Skip to content

useTestHooksOnTop (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"style": {
"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/style/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.

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

See also: useTestHooksInOrder