vitest-dev / vitest-dev/vitest

Add `onCleanup` hook

Open
#7,258 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

p2-nice-to-have
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Clear and concise description of the problem

There are times when the test runners needs to run a cleanup callback manually and not tied it to the test runner lifecycle (avoid beforeEach/afterEach). A good example of this is calling the cleanup when switching tests in the Browser Mode UI.

Suggested solution

@vitest/runner can expose an onCleanup hook. Consumers of the runner can decide when to run passed down callbacks. By default, Vitest will run those callbacks after each test (when running tests in the Node.js environment). Vitest Browser Mode will also run those hooks after each test, except for the last one, so the UI is still available.

Alternative

No response

Additional context

See #7141 for more details

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the context in #7141 and the @vitest/runner lifecycle, then trace how cleanup currently runs in Node.js and Browser Mode. Determine the entry points that pass cleanup callbacks and how each environment schedules them. Done means consumers can trigger the hook manually while the default per-test behavior and Browser Mode’s last-test exception remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.