useTestHooksInOrder
Summary
Section titled “Summary”- Rule available since:
v2.4.15 - Diagnostic Category:
lint/nursery/useTestHooksInOrder - 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-in-order - Same as
vitest/prefer-hooks-in-order - Same as
playwright/prefer-hooks-in-order
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useTestHooksInOrder": "error" } } }}Description
Section titled “Description”Enforce that test lifecycle hooks are declared in the order they execute.
Jest and Vitest always execute lifecycle hooks in the following order, regardless of how they are written in the file:
beforeAll(orbeforeif you are usingnode:test)beforeEachafterEachafterAll(orafterif you are usingnode:test)
Writing the hooks in a different order creates a discrepancy between the visual order in the source and the actual execution order, which makes test code harder to reason about.
This rule flags any hook that appears after a hook that runs later in the execution order. Only consecutive groups of hooks in the same block are compared — test cases and other statements between hooks are allowed and reset the comparison baseline.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”describe('foo', () => { beforeEach(() => {}); beforeAll(() => {});});code-block.js:3:3 lint/nursery/useTestHooksInOrder ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ beforeAll is out of order compared to beforeEach.
1 │ describe(‘foo’, () => {
2 │ beforeEach(() => {});
> 3 │ beforeAll(() => {});
│ ^^^^^^^^^^^^^^^^^^^
4 │ });
5 │
ℹ beforeEach is declared here but executes after beforeAll.
1 │ describe(‘foo’, () => {
> 2 │ beforeEach(() => {});
│ ^^^^^^^^^^^^^^^^^^^^
3 │ beforeAll(() => {});
4 │ });
ℹ Reorder the lifecycle hooks to appear in the order they execute: beforeAll/before, beforeEach, afterEach, afterAll/after.
ℹ 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', () => { afterEach(() => {}); afterAll(() => {}); beforeAll(() => {});});code-block.js:4:3 lint/nursery/useTestHooksInOrder ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ beforeAll is out of order compared to afterAll.
2 │ afterEach(() => {});
3 │ afterAll(() => {});
> 4 │ beforeAll(() => {});
│ ^^^^^^^^^^^^^^^^^^^
5 │ });
6 │
ℹ afterAll is declared here but executes after beforeAll.
1 │ describe(‘foo’, () => {
2 │ afterEach(() => {});
> 3 │ afterAll(() => {});
│ ^^^^^^^^^^^^^^^^^^
4 │ beforeAll(() => {});
5 │ });
ℹ Reorder the lifecycle hooks to appear in the order they execute: beforeAll/before, beforeEach, afterEach, afterAll/after.
ℹ 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', () => { beforeAll(() => {}); beforeEach(() => {}); afterEach(() => {}); afterAll(() => {});});// Hooks separated by test cases are treated independently.describe('foo', () => { beforeEach(() => {}); it('a test', () => {}); afterAll(() => {});});See useTestHooksOnTop if you want to group all the hooks at the top of the block, before any test cases.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.