hooksBeforeTestCases
Enforces that all lifecycle hooks should come before all test cases.
✅ This rule is included in the vitest stylisticStrictpresets.
Vitest runs setup and teardown hooks for every test case in their scope, regardless of where they are declared. Some repositories prefer to declare hooks before all test cases, so the setup/teardown that tests rely on is visible above them.
This rule reports on any hook declared after a test case in the same scope.
Examples
Section titled “Examples”describe("math", () => { it("adds values", () => {});
beforeEach(() => { // ... });});describe("math", () => { beforeEach(() => { // ... });
it("adds values", () => {});});Options
Section titled “Options”This rule is not configurable.
When Not to Use It
Section titled “When Not to Use It”If you prefer to declare hooks close to the test cases that use them, this rule might not be for you. Because Vitest applies hooks to every test in their scope no matter their placement, the ordering is a matter of readability rather than correctness.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useTestHooksOnTop - ESLint:
vitest/prefer-hooks-on-top - Oxlint:
vitest/prefer-hooks-on-top
