Hooks
| Hook | Signature | Scope | Notes |
|---|---|---|---|
beforeAll(fn, timeout?) |
fn: (ctx: ModuleContext) => unknown |
File (top-level) or enclosing describe |
Runs once before all tests in scope. |
afterAll(fn, timeout?) |
same | same | Runs once after all tests in scope. |
beforeEach(fn, timeout?) |
fn: (ctx: TestContext) => unknown |
File or enclosing describe |
Runs before every test in scope; may return a cleanup function, which runs after all afterEach hooks in that scope (i.e. later than ordinary afterEach). |
afterEach(fn, timeout?) |
same | same | Runs after every test in scope. |
onTestFinished(fn) |
fn: (ctx: TestContext) => unknown, called from inside a running test/hook |
Single test instance | Runs after that test’s afterEach chain; multiple registrations run in reverse registration order; not affected by sequence.hooks config. |
onTestFailed(fn) |
same shape | Single test instance | Only fires if the test failed; also runs after afterEach. |
onTestFinished and onTestFailed have two call forms
Section titled “onTestFinished and onTestFailed have two call forms”Both are destructurable off
TestContext (as shown in signature variants) and importable directly from 'vitest' as named exports, the same
shape as beforeEach/afterEach. Both forms throw if called outside a running test body. The
docs explicitly warn the global/imported form is unsound under .concurrent: “If you are running
tests concurrently, you should always use the onTestFailed/onTestFinished hook from the test
context since Vitest doesn’t track concurrent tests in global hooks”, i.e. the imported form
resolves “the current test” through shared module state that a single sequential runner keeps
consistent but that concurrent execution does not. An extractor doing name-based import resolution
for 'vitest' exports should recognize onTestFinished/onTestFailed as legal top-level imports,
not just as context members.
Nesting and ordering
Section titled “Nesting and ordering”- Hooks registered at file top-level apply to every test in the file; hooks registered inside a
describeapply only to tests (and nested describes) within that block. - Default execution order is a “stack”:
beforeAll/beforeEachrun outer-to-inner (parent before child),afterAll/afterEachrun inner-to-outer (child before parent), i.e.afterEachorder is the reverse ofbeforeEachregistration order, configurable globally viasequence.hooks. test.beforeEach/test.afterEach/test.beforeAll/test.afterAll(hooks hung off any test function,test.extend()-produced or not) are the top-level hook functions themselves, scoped to whatever suite they’re declared in, so a modifier chained before one (test.skip.beforeEach) does not apply. On an extended test they can see fixture types.test.aroundEach/test.aroundAll(v4.1+) combine setup+teardown in one wrapper; exact signature UNVERIFIED, treat as an opaque hook-registering call for extraction purposes (record its presence and scope; don’t try to model control flow inside it yet).
The effective hook chain
Section titled “The effective hook chain”For context-compatibility purposes (the redundant-hook and context-equivalence rules), a
TestCase’s effective hook chain is: every
beforeAll/beforeEach from the root of the file down through each enclosing describe, in
declaration order, plus the corresponding afterEach/afterAll from each level in reverse,
this is exactly the ordered set the analyzer needs to compare for “equivalent context.”
A registration written under control flow – inside an if branch, a loop body, a
try/catch/finally arm or a switch case (including fallthrough between arms), or after a
return/throw/break/continue among direct siblings of its block – carries that position
as part of its identity. The guard is recorded structurally (which position)
plus, where one exists, the controlling expression’s own source text; it is never an evaluation
of which branch runs. Different guards can both be true for the same tests, and a matching
switch case can fall through to later cases – the identity is structural, never a claim of
exclusive execution. Because the runtime registration depends on that unevaluated control flow
and on the values of the conditions governing it, a scope holding any guarded registration is
marked unresolved: its tests are excluded from comparison rather than compared on an identity
that cannot know whether the hook runs.
Exit tracking applies equally to braced blocks and single-statement bodies.
A local break or continue at the end of a block does not erase an earlier possible return, or
a throw under a try arm (see below for throws nothing catches). Hooks after the enclosing construct remain uncertain when any such path can skip them.
A conditional break stops guarding later registrations once its known label destination has
finished. A return, or a throw under a try arm, inside a labeled block can still skip hooks
after that block; mixed
or unknown exit destinations retain their uncertainty.
An exit after a nested suite registration does not make that suite’s hooks conditional. A parent hook registered after an uncertain exit still affects every descendant test, regardless of where the child suite was registered.
A return inside an expanded local helper ends that helper, so it does not make later caller hooks conditional. Hooks registered under control flow inside the helper remain unresolved.
A throw that nothing catches during collection fails the whole file in Vitest: no test runs, so
it cannot skip a hook for a test that does. A conditional exit that can only throw therefore
guards nothing when no try arm encloses it – in the module, a suite callback, or an expanded
helper and its callers up to the nearest suite callback. A try around describe(...) does not
catch its callback’s throw, because the callback runs later. Under any try arm, a throw that can
escape to later hooks keeps its uncertainty, as does a return; handlers are not evaluated to
prove what they catch.
Back to the Vitest surface index.