useTestHooksOnTop
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useTestHooksOnTop - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
- Sources:
- Same as
jest/prefer-hooks-on-top - Same as
vitest/prefer-hooks-on-top - Same as
playwright/prefer-hooks-on-top
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useTestHooksOnTop": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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', () => {});});Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.