Skip to content

Conversation

@yamachi4416
Copy link
Collaborator

@yamachi4416 yamachi4416 commented Dec 15, 2025

πŸ”— Linked issue

this isn't a direct fix, but it now supports the use case in the issue below.
#1496 #1185

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

I explored whether we could mock the modules used in our global middleware using the existing setupFile mechanism that starts the NuxtApp. However, I realized that this approach couldn't support both scenarios: mocking the module when the NuxtApp starts and mocking it after it has already started.

To resolve this, I introduced a new startOn option. This allows us to choose the timing for starting the NuxtApp, which can now be toggled per test file using @vitest-environment-options.

Reproductions

stackblitz for issue 1185
expected: eventBus $on EVENT XXX, the XXX values match, and app.vue - onEvent() is logged.

eventBus $on EVENT 660
eventBus $on EVENT 660
eventBus $emit EVENT 660
app.vue - onEvent() {msg: 'utils/eventBus'}
index.vue - onEvent() {msg: 'utils/eventBus'

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 15, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@nuxt/test-utils@1516

commit: e71dfc7

@yamachi4416 yamachi4416 marked this pull request as ready for review December 15, 2025 11:50
@yamachi4416
Copy link
Collaborator Author

I’ve realized there are some points I haven't fully considered yet, so I’ll revert this to a draft for further refinement. My apologies.

@yamachi4416 yamachi4416 marked this pull request as draft December 16, 2025 11:34
@yamachi4416 yamachi4416 force-pushed the feat/support-mock-in-middleware branch from 1823a5c to c2fbe7b Compare December 19, 2025 12:08
@yamachi4416 yamachi4416 changed the title fix(runtime): make mockNuxtImport work inside middleware feat(runtime): add startOn environment option Dec 19, 2025
@yamachi4416 yamachi4416 force-pushed the feat/support-mock-in-middleware branch from c2fbe7b to 4a89806 Compare December 19, 2025 16:40
@yamachi4416 yamachi4416 force-pushed the feat/support-mock-in-middleware branch from 4a89806 to 575fbba Compare December 19, 2025 17:02
@yamachi4416 yamachi4416 force-pushed the feat/support-mock-in-middleware branch 2 times, most recently from 7e235ea to 297fb17 Compare January 6, 2026 12:43
@danielroe
Copy link
Member

danielroe commented Jan 6, 2026

@yamachi4416 are you on Discord or Bluesky/Twitter? would you mind sending me a DM (nothing bad!)?

@yamachi4416
Copy link
Collaborator Author

It turns out that referencing __vitest_worker__ was indeed a hack, and it didn't work in vitest v4...

@yamachi4416 yamachi4416 force-pushed the feat/support-mock-in-middleware branch 2 times, most recently from 9104a83 to 145cf17 Compare January 9, 2026 12:43
@yamachi4416 yamachi4416 force-pushed the feat/support-mock-in-middleware branch from 145cf17 to e71dfc7 Compare January 10, 2026 03:49
@yamachi4416
Copy link
Collaborator Author

I wish I could have supported @vitest-environment-options, but I have created a separate issue for that.

@yamachi4416 yamachi4416 marked this pull request as ready for review January 10, 2026 05:00
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