Skip to content

fix(vitest): afterEach not called early when sequence.hooks is set#327

Merged
AriPerkkio merged 1 commit intomainfrom
ari/fix-after-each
May 1, 2026
Merged

fix(vitest): afterEach not called early when sequence.hooks is set#327
AriPerkkio merged 1 commit intomainfrom
ari/fix-after-each

Conversation

@AriPerkkio
Copy link
Copy Markdown
Member

@AriPerkkio AriPerkkio commented Apr 29, 2026

Issue:

What Changed

In Material UI they have setup file that cleans DOM in afterEach. They also use sequence.hooks: "list", which made their setup file run before ours. This caused automatic snapshots to be empty.

Now we'll look for sequence.hooks config when deciding whether setup file should be in the start of end. Also noticed Vitest bug (or feature) where callbacks returned from beforeEach(() => () => void) were always run last. Now using explicit afterEach to work-around that.

How to test

Added test cases that fail without the fix. Revert the changes and see tests fail.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 29, 2026

⚠️ No Changeset found

Latest commit: 7a90b6c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 84.61538% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.48%. Comparing base (89f7beb) to head (7a90b6c).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
packages/vitest/src/browser/setupFile.ts 77.77% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #327      +/-   ##
==========================================
+ Coverage   89.96%   91.48%   +1.52%     
==========================================
  Files          22       22              
  Lines         598      611      +13     
  Branches      109      114       +5     
==========================================
+ Hits          538      559      +21     
+ Misses         56       49       -7     
+ Partials        4        3       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread packages/vitest/src/node/plugin.ts Outdated
@AriPerkkio AriPerkkio self-assigned this Apr 29, 2026
@AriPerkkio AriPerkkio changed the base branch from ari/fix-resolve-root-from-root-config to main April 30, 2026 12:05
@AriPerkkio AriPerkkio marked this pull request as ready for review April 30, 2026 12:10
@AriPerkkio AriPerkkio changed the title fix(vitest): afterEach not called when sequence.hooks is set fix(vitest): afterEach not called early when sequence.hooks is set Apr 30, 2026
@AriPerkkio AriPerkkio merged commit 946a9e6 into main May 1, 2026
10 of 12 checks passed
@AriPerkkio AriPerkkio deleted the ari/fix-after-each branch May 1, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants