Skip to content

fix(test): exclude @vitest/browser/context from vendor-aliases to fix missing server export#1110

Merged
fengmk2 merged 3 commits intovoidzero-dev:mainfrom
kazupon:fix/1086
Mar 23, 2026
Merged

fix(test): exclude @vitest/browser/context from vendor-aliases to fix missing server export#1110
fengmk2 merged 3 commits intovoidzero-dev:mainfrom
kazupon:fix/1086

Conversation

@kazupon
Copy link
Collaborator

@kazupon kazupon commented Mar 23, 2026

Summary

Fixes the missing server export from @vitest/browser/context when using @storybook/addon-vitest with vite-plus.

resolve #1086

Root Cause

In vitest's browser mode, @vitest/browser/context is a virtual module, the BrowserContext plugin intercepts the bare specifier in resolveId and returns dynamically generated code (via generateContextFile()) that includes the server export with command RPC proxies, config, platform info, etc.

However, vite-plus injects a vitest:vendor-aliases plugin during re-packaging (in build.ts) that maps @vitest/* bare specifiers to static files in the dist directory.

Both plugins use enforce: 'pre', but vendor-aliases is registered before BrowserContext in the plugin array.

Since Vite executes same-enforce plugins in registration order, vendor-aliases resolves the bare specifier to the static context.js file before BrowserContext ever sees it.

The static context.js only exports { cdp, createUserEvent, locators, page, utils } — it does not include server, because that's supposed to come from the virtual module.

Normal flow (plain vitest — no vendor-aliases)

sequenceDiagram
    participant Browser
    participant ViteServer as Vite Browser Server
    participant BC as BrowserContext Plugin<br/>(resolveId)
    participant Gen as generateContextFile()

    Browser->>ViteServer: import "@vitest/browser/context"
    ViteServer->>BC: resolveId("@vitest/browser/context")
    BC-->>ViteServer: "\0vitest/browser" (virtual module ID)
    ViteServer->>BC: load("\0vitest/browser")
    BC->>Gen: generateContextFile()
    Gen-->>BC: Dynamic code with server, commands, config...
    BC-->>ViteServer: Virtual module source
    ViteServer-->>Browser: Module with { server, page, cdp, ... }
Loading

Bug flow (vite-plus — vendor-aliases intercepts first)

sequenceDiagram
    participant Browser
    participant ViteServer as Vite Browser Server
    participant VA as vendor-aliases Plugin<br/>(resolveId) ← runs first
    participant BC as BrowserContext Plugin<br/>(resolveId) ← never called

    Browser->>ViteServer: import "@vitest/browser/context"
    ViteServer->>VA: resolveId("@vitest/browser/context")
    VA-->>ViteServer: "/path/to/dist/@vitest/browser/context.js" (static file)
    Note over BC: resolveId is never called<br/>because vendor-aliases already resolved it
    ViteServer-->>Browser: Static file (no server export)
    Browser->>Browser: SyntaxError: does not provide<br/>an export named 'server'
Loading

Plugin registration order in the browser Vite server

1. vitest:vendor-aliases              ← enforce:'pre', registered first
2. vitest:browser                     ← enforce:'pre'
3. vitest:browser:tests               ← enforce:'pre'
4. vitest:browser:virtual-module:context (BrowserContext) ← enforce:'pre', registered last

Fix

Exclude @vitest/browser/context from the vendor-aliases plugin's vendorMap during re-packaging (build.ts).

This allows the bare specifier to pass through to the BrowserContext plugin, which correctly returns the virtual module with the server export.

@netlify
Copy link

netlify bot commented Mar 23, 2026

Deploy Preview for viteplus-preview canceled.

Name Link
🔨 Latest commit 67c9781
🔍 Latest deploy log https://app.netlify.com/projects/viteplus-preview/deploys/69c0ce0ae27e530008f8e44e

@kazupon
Copy link
Collaborator Author

kazupon commented Mar 23, 2026

@codex review

@kazupon kazupon requested review from fengmk2 and sheremet-va March 23, 2026 03:27
@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@fengmk2 fengmk2 added the test: e2e Auto run e2e tests label Mar 23, 2026
@fengmk2 fengmk2 merged commit c75d8df into voidzero-dev:main Mar 23, 2026
47 checks passed
@kazupon kazupon deleted the fix/1086 branch March 23, 2026 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test: e2e Auto run e2e tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vp test fails with missing server export from @vitest/browser/context when using @storybook/addon-vitest

2 participants