Skip to content

fix(storybook-addon-vis): await the vitest module load before using the proxies - #849

Merged
unional merged 1 commit into
mainfrom
cyberlegion/unit-214a0f5315723a00
Aug 21, 2026
Merged

fix(storybook-addon-vis): await the vitest module load before using the proxies#849
unional merged 1 commit into
mainfrom
cyberlegion/unit-214a0f5315723a00

Conversation

@unional

@unional unional commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Closes #835.

Problem

src/client/vitest_proxy.ts populated commands and page from a dynamic import that nothing awaited. Until it settled, browserContext was undefined and every property read through the commands proxy returned undefined — so the addon's beforeAll reading commands.setupVisSuite could fail with commands.setupVisSuite is not a function inside a run that genuinely is vitest browser mode. Ordering, not detection; the #829 guard is untouched.

Why the import stays dynamic

src/index.ts is the Storybook preview annotation entry, so this module is also evaluated in a plain Storybook preview where vitest and vitest/browser are not available. Hoisting to a static import (or a top-level await) would break that, so the fix enforces the ordering instead of removing the asynchrony.

Fix

  • A command read before the import settles now returns a function that awaits the load and then delegates. Browser commands are async RPC calls, so this closes the window for every consumer of the proxy — including vis.setup() via storybook-addon-vis/vitest-setup — without each call site opting in.
  • whenVitestProxyReady() exposes the load, and the addon's beforeAll awaits it before page and getCurrentTest (both read synchronously, so they cannot defer themselves).
  • Outside a vitest browser run nothing is loaded and commands keeps yielding undefined, so the isRunningInTest check causes problems during agentic debugging #829 guards stay in charge.

Test

The timing is not observable on the module-level proxy (its import has long settled by the time a test runs), so createVitestProxy is exported and driven with loaders that stay pending until the test resolves them — the ordering is proven directly rather than raced.

Reverting only the proxy change turns 3 of the 7 new tests red (expected 'undefined' to be 'function'). Full package suite: 83 passed, plus pnpm build, tsc --noEmit, and biome check clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NLB1dHgUEekHmMNj4G7R63

…he proxies

`commands` and `page` are populated by a dynamic import that nothing awaited, so
a hook reading `commands.setupVisSuite` before it settled got `undefined` and
failed with `commands.setupVisSuite is not a function` — inside a run that
genuinely is vitest browser mode. The imports stay dynamic (this module is also
reached from a plain Storybook preview, where the vitest modules do not exist),
but the ordering is now enforced:

- a command read before the import settles returns a function that waits for it
  and then delegates, instead of `undefined`
- `whenVitestProxyReady()` exposes the load, and the addon's `beforeAll` awaits
  it before reading `page` or the current test

`createVitestProxy` is exported so the pending window can be driven directly in
a test; the module-level proxy's import has long settled by then.

Closes #835

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NLB1dHgUEekHmMNj4G7R63
@changeset-bot

changeset-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f450ead

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
storybook-addon-vis Patch

Not sure what this means? Click here to learn what changesets are.

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

@unional
unional merged commit ec02e44 into main Aug 21, 2026
10 checks passed
@unional
unional deleted the cyberlegion/unit-214a0f5315723a00 branch August 21, 2026 05:52
@github-actions github-actions Bot mentioned this pull request Aug 21, 2026
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.

Vitest browser command proxy can still be empty when the addon's beforeAll runs

1 participant