Skip to content

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.

describe("math", () => {
it("adds values", () => {});
beforeEach(() => {
// ...
});
});

This rule is not configurable.

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.