vitest-dev / vitest-dev/vitest
Add `onCleanup` hook
Nobody has claimed this yet.
- 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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