Skip to content

Addon-Vitest: Use Vitest's provide-API for injecting values#34518

Merged
JReinhold merged 8 commits into
nextfrom
jeppe/addon-vitest-provide
Apr 30, 2026
Merged

Addon-Vitest: Use Vitest's provide-API for injecting values#34518
JReinhold merged 8 commits into
nextfrom
jeppe/addon-vitest-provide

Conversation

@JReinhold
Copy link
Copy Markdown
Contributor

@JReinhold JReinhold commented Apr 10, 2026

Closes #

What I did

This PR changes the logic for injecting configuration into Vitest runs. Previously, we were setting process.env, but now we're using Vitest's provide+inject API, which was built for this exact purpose.
The structure is also more generic, allowing for arbitrary values to be injected now. Before, we hardcoded the configurations of the accessibility and coverage options, but now anything is injectable by overriding the run config. In stories, the injected values are available as globals['storybook/test-provided]. Addon MCP might use this soon, to customise run options and add additional reports based on that.
It's also built in a way for any users to do the same thing, although the use case isn't clear yet, so let's not document it just yet.

See:

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

Run component tests through both the Vitest CLI and the Testing Module UI and see that it behaves as it always does. In the UI, try out different configurations of a11y and coverage.

I've also tried this with Vitest 3.0.9 to ensure it works with the older Vitest versions we support (v3.0.0 doesn't support the --project flag that we rely on), using the following package.json and Vitest config:

package.json
    "@vitejs/plugin-react": "^5",
    "@vitest/browser": "~3.0.0",
    "@vitest/coverage-v8": "~3.0.0",
    ...,
    "vite": "^6",
    "vitest": "~3.0.0"
Vite config
/// <reference types="vitest/config" />
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vite.dev/config/
import path from "node:path";
import { fileURLToPath } from "node:url";
import { storybookTest } from "@storybook/addon-vitest/vitest-plugin";
const dirname =
	typeof __dirname !== "undefined" ? __dirname : path.dirname(fileURLToPath(import.meta.url));

// More info at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon
export default defineConfig({
	plugins: [react()],
	resolve: {
		preserveSymlinks: true,
	},
	server: {
		fs: {
			allow: ["../../.."],
		},
	},
	test: {
		workspace: [
			{
				extends: true,
				plugins: [
					// The plugin will run tests for the stories defined in your Storybook config
					// See options at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon#storybooktest
					storybookTest({
						configDir: path.join(dirname, ".storybook"),
						tags: {
							include: ["vitest"],
						},
					}),
				],
				test: {
					name: "storybook",
					browser: {
						enabled: true,
						headless: true,
						provider: "playwright",
						instances: [
							{
								browser: "chromium",
							},
						],
					},
				},
			},
		],
	},
});

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This pull request has been released as version 0.0.0-pr-34518-sha-4a90f0ac. Try it out in a new sandbox by running npx storybook@0.0.0-pr-34518-sha-4a90f0ac sandbox or in an existing project with npx storybook@0.0.0-pr-34518-sha-4a90f0ac upgrade.

More information
Published version 0.0.0-pr-34518-sha-4a90f0ac
Triggered by @JReinhold
Repository storybookjs/storybook
Branch jeppe/addon-vitest-provide
Commit 4a90f0ac
Datetime Thu Apr 30 11:01:03 UTC 2026 (1777546863)
Workflow run 25161775419

To request a new release of this pull request, mention the @storybookjs/core team.

core team members can create a new canary release here or locally with gh workflow run --repo storybookjs/storybook publish.yml --field pr=34518

Summary by CodeRabbit

  • Refactor

    • Inject runtime test configuration into the test runner and derive coverage/a11y from that run config; reruns reprovide updated config before execution.
  • New Features

    • Persist and merge multiple report types per story so all reports are retained across runs.
    • Expose runtime flags for ghost-stories and render-analysis to the test environment.
  • Tests

    • Added tests for config overrides, watch-triggered reruns refreshing config, and multi-report persistence.

JReinhold and others added 3 commits April 10, 2026 11:06
… configuration into the browser environment. allow any run config overrides. store all reports on currentRun, not just a11yReports
Co-authored-by: Copilot <copilot@github.com>
@JReinhold JReinhold self-assigned this Apr 22, 2026
@JReinhold JReinhold requested review from AriPerkkio and yannbf April 22, 2026 09:20
@JReinhold JReinhold added maintenance User-facing maintenance tasks ci:normal addon: vitest labels Apr 22, 2026
@storybook-app-bot
Copy link
Copy Markdown

storybook-app-bot Bot commented Apr 22, 2026

Package Benchmarks

Commit: 4a90f0a, ran on 30 April 2026 at 11:12:20 UTC

No significant changes detected, all good. 👏

@JReinhold JReinhold changed the title Addon-Vitest: Allow arbitrary run config and reports Addon-Vitest: Use Vitest's provide-API for injecting values Apr 22, 2026
@JReinhold JReinhold marked this pull request as ready for review April 22, 2026 12:06
@JReinhold JReinhold requested a review from Copilot April 22, 2026 12:06
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 54d26d59-19d9-4aa0-97a8-d5e06c509b10

📥 Commits

Reviewing files that changed from the base of the PR and between 05667ea and 4a90f0a.

📒 Files selected for processing (18)
  • .external/addon-svelte-csf
  • .external/addon-webpack5-compiler-babel
  • .external/addon-webpack5-compiler-swc
  • .rollout-repos/addon-coverage
  • .rollout-repos/addon-designs
  • .rollout-repos/addon-kit
  • .rollout-repos/addon-styling-webpack
  • .rollout-repos/addon-visual-tests
  • .rollout-repos/addon-webpack5-compiler-babel
  • .rollout-repos/addon-webpack5-compiler-swc
  • .rollout-repos/icons
  • .rollout-repos/telejson
  • .rollout-repos/test-runner
  • .rollout-repos/vite-plugin-storybook-nextjs
  • code/addons/vitest/src/constants.ts
  • code/addons/vitest/src/vitest-plugin/index.ts
  • code/addons/vitest/src/vitest-plugin/test-utils.ts
  • code/addons/vitest/src/vitest-provided-context.d.ts
✅ Files skipped from review due to trivial changes (13)
  • .external/addon-webpack5-compiler-babel
  • .rollout-repos/addon-designs
  • .external/addon-webpack5-compiler-swc
  • .external/addon-svelte-csf
  • .rollout-repos/addon-visual-tests
  • .rollout-repos/addon-webpack5-compiler-babel
  • .rollout-repos/addon-webpack5-compiler-swc
  • .rollout-repos/test-runner
  • .rollout-repos/telejson
  • .rollout-repos/addon-styling-webpack
  • .rollout-repos/vite-plugin-storybook-nextjs
  • .rollout-repos/addon-coverage
  • .rollout-repos/icons
🚧 Files skipped from review as they are similar to previous changes (3)
  • code/addons/vitest/src/vitest-provided-context.d.ts
  • code/addons/vitest/src/constants.ts
  • code/addons/vitest/src/vitest-plugin/test-utils.ts

📝 Walkthrough

Walkthrough

Replaces browser getInitialGlobals with Vitest provide/inject for run config and feature flags, broadens run/event config typing to Record<string, unknown>, adds runtime currentRun.reports to accumulate multiple report types, and updates managers, plugin, test-utils, types, and tests to use the new provide/inject flow.

Changes

Cohort / File(s) Summary
Types & Constants
code/addons/vitest/src/types.ts, code/addons/vitest/src/constants.ts, code/addons/vitest/src/vitest-provided-context.d.ts
Add RunConfig = Record<string, unknown>, export provide keys (STORYBOOK_TEST_PROVIDE_KEY, STORYBOOK_CORE_GHOST_STORIES_PROVIDE_KEY, STORYBOOK_CORE_RENDER_ANALYSIS_PROVIDE_KEY), derive A11yRunReport from Report, broaden run/config typings to RunConfig, and add Vitest ProvidedContext augmentation for the new keys.
Vitest runtime integration
code/addons/vitest/src/node/vitest-manager.ts
Inject/provide current run config into Vitest via vitest.provide(STORYBOOK_TEST_PROVIDE_KEY, ...), derive coverage from currentRun.config, and re-provide config on watch-triggered reruns.
Test manager (logic & tests)
code/addons/vitest/src/node/test-manager.ts, code/addons/vitest/src/node/test-manager.test.ts
Change runTestsWithState/run config types to RunConfig, remove per-run env var setting, accumulate multiple report types keyed by storyId into currentRun.reports while preserving legacy a11yReports, and add tests verifying vitest.provide(...), config override/merge behavior, watch re-provide, and report persistence.
Plugin & test-utils runtime delivery
code/addons/vitest/src/vitest-plugin/index.ts, code/addons/vitest/src/vitest-plugin/test-utils.ts
Replace getInitialGlobals browser flow with Vite provide/Vitest inject, synchronously derive initialGlobals from injected values (including run config and ghost/render flags), remove reliance on @vitest/browser/context, and adapt composeStory/test composition accordingly.
Submodule pointer updates
.external/*, .rollout-repos/*
Advance multiple git submodule pointers to new commits; no code API changes in these submodule pointer updates.

Sequence Diagram

sequenceDiagram
    participant Store as Store / TestManager
    participant VitestMgr as Vitest Manager
    participant Vitest as Vitest runtime
    participant Plugin as Vitest Plugin
    participant TestUtils as Test Utils / composeStory

    Store->>VitestMgr: set currentRun.config
    VitestMgr->>Vitest: provide(STORYBOOK_TEST_PROVIDE_KEY, RunConfig)
    VitestMgr->>Vitest: provide(STORYBOOK_CORE_GHOST_STORIES_PROVIDE_KEY, boolean)
    Plugin->>Vitest: configure provides at build/init
    TestUtils->>Vitest: inject(STORYBOOK_TEST_PROVIDE_KEY)
    Vitest-->>TestUtils: RunConfig
    TestUtils->>TestUtils: build initialGlobals (runConfig, ghostStories, renderAnalysis)
    TestUtils->>composeStory: call composeStory(initialGlobals)
    Vitest->>VitestMgr: run test specifications (uses provided RunConfig)
    Vitest->>VitestMgr: onTestCaseResult (report)
    VitestMgr->>Store: merge reports into currentRun.reports
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Review rate limit: 4/5 reviews remaining, refill in 12 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@code/addons/vitest/src/vitest-provided-context.d.ts`:
- Around line 1-13: The interface augmentation for ProvidedContext uses computed
property names with the exported constants STORYBOOK_TEST_PROVIDE_KEY and
STORYBOOK_CORE_GHOST_STORIES_PROVIDE_KEY (in vitest-provided-context.d.ts),
which is invalid TypeScript; replace those computed keys with the actual string
literal keys (e.g. the literal values those constants represent such as
'storybook/test-provided' and the ghost-stories key) or change the shape to an
index signature (e.g. add a specific string-keyed property or a Record<string,
unknown> entry) inside the declare module 'vitest' block so ProvidedContext does
not use variable-based computed property names. Ensure you reference the
ProvidedContext interface and the two constant names when making the change so
the augmentation matches the runtime keys.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2335ebf3-fd25-49ef-8818-ec34bf9eb483

📥 Commits

Reviewing files that changed from the base of the PR and between ff98f86 and 1925ba5.

📒 Files selected for processing (8)
  • code/addons/vitest/src/constants.ts
  • code/addons/vitest/src/node/test-manager.test.ts
  • code/addons/vitest/src/node/test-manager.ts
  • code/addons/vitest/src/node/vitest-manager.ts
  • code/addons/vitest/src/types.ts
  • code/addons/vitest/src/vitest-plugin/index.ts
  • code/addons/vitest/src/vitest-plugin/test-utils.ts
  • code/addons/vitest/src/vitest-provided-context.d.ts

Comment thread code/addons/vitest/src/vitest-provided-context.d.ts
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates @storybook/addon-vitest to inject Storybook run configuration into Vitest via Vitest’s provide/inject API instead of relying on process.env, and expands run reporting to support arbitrary report types (while keeping legacy a11y reports).

Changes:

  • Replace env-based config passing with Vitest provide (manager) + inject (test runtime) to build initialGlobals.
  • Add generic reports persistence to currentRun while retaining a11yReports for backwards compatibility.
  • Introduce provide keys in constants and wire ghost-stories enablement through Vitest config provide.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
code/addons/vitest/src/vitest-provided-context.d.ts Adds Vitest ProvidedContext augmentation for Storybook-provided keys.
code/addons/vitest/src/vitest-plugin/test-utils.ts Switches globals initialization to read injected values via inject() and sets initialGlobals accordingly.
code/addons/vitest/src/vitest-plugin/index.ts Removes browser command-based globals and uses Vitest config provide for ghost-stories.
code/addons/vitest/src/types.ts Introduces RunConfig, adds reports, and updates run-state typing.
code/addons/vitest/src/node/vitest-manager.ts Calls vitest.provide() with the current run config before running tests.
code/addons/vitest/src/node/test-manager.ts Stops writing run config into process.env, and persists generic reports alongside a11y reports.
code/addons/vitest/src/node/test-manager.test.ts Adds assertions for vitest.provide() and for persisting generic reports.
code/addons/vitest/src/constants.ts Adds provide keys and extends initial state with reports; broadens trigger payload config typing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread code/addons/vitest/src/types.ts
Comment thread code/addons/vitest/src/node/vitest-manager.ts
Copy link
Copy Markdown
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
code/addons/vitest/src/node/test-manager.test.ts (1)

243-243: Consolidate inline globTestSpecifications mock implementations to beforeEach block.

This mock behavior is set inline within test bodies at lines 224, 243, 266, 354, 369, 384, 401, 415, and 439. Since all occurrences use the identical implementation mockImplementation(() => tests), move this to the existing beforeEach block and access via vi.mocked() for type-safe consistency with repo Vitest conventions as defined in the coding guidelines.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/addons/vitest/src/node/test-manager.test.ts` at line 243, Move the
repeated inline mocks of vitest.globTestSpecifications into the existing
beforeEach: replace all occurrences of
vitest.globTestSpecifications.mockImplementation(() => tests) inside individual
tests with a single setup in beforeEach that uses
vi.mocked(vitest.globTestSpecifications).mockImplementation(() => tests) (or
vi.mocked(vitest).globTestSpecifications.mockImplementation(() => tests)
depending on how vitest is mocked in this file) to provide type-safe mocking per
repo Vitest conventions, and remove the redundant inline mock lines in tests
that reference vitest.globTestSpecifications.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@code/addons/vitest/src/node/test-manager.test.ts`:
- Line 243: Move the repeated inline mocks of vitest.globTestSpecifications into
the existing beforeEach: replace all occurrences of
vitest.globTestSpecifications.mockImplementation(() => tests) inside individual
tests with a single setup in beforeEach that uses
vi.mocked(vitest.globTestSpecifications).mockImplementation(() => tests) (or
vi.mocked(vitest).globTestSpecifications.mockImplementation(() => tests)
depending on how vitest is mocked in this file) to provide type-safe mocking per
repo Vitest conventions, and remove the redundant inline mock lines in tests
that reference vitest.globTestSpecifications.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: af5923ae-12c0-44b9-bd33-728ad82a9280

📥 Commits

Reviewing files that changed from the base of the PR and between 1925ba5 and 05667ea.

📒 Files selected for processing (2)
  • code/addons/vitest/src/node/test-manager.test.ts
  • code/addons/vitest/src/node/vitest-manager.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • code/addons/vitest/src/node/vitest-manager.ts

JReinhold and others added 2 commits April 30, 2026 12:30
@JReinhold JReinhold merged commit e684125 into next Apr 30, 2026
124 checks passed
@JReinhold JReinhold deleted the jeppe/addon-vitest-provide branch April 30, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

addon: vitest ci:normal maintenance User-facing maintenance tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants