Skip to content

AddonVitest: Improve perf & fix loading incorrect .env file#33469

Merged
ndelangen merged 4 commits into
nextfrom
norbert/improve-vitest-addon-plugin
Jan 6, 2026
Merged

AddonVitest: Improve perf & fix loading incorrect .env file#33469
ndelangen merged 4 commits into
nextfrom
norbert/improve-vitest-addon-plugin

Conversation

@ndelangen
Copy link
Copy Markdown
Member

@ndelangen ndelangen commented Jan 6, 2026

Closes #33101

What I did

I am doing 2 things in this PR:

1: I'm improving the performance of the addon-vitest package, by only evaluating what config to add/change when the code is being called in a vitest environment.
2: Fix a bug where the incorrect .env file is loaded

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

I discussed with @JReinhold about how to approach testing this, and though we discussed options like, extending a sandbox in some way or writing a unit-test for the function, both would be rather hard to do.

So I'm going to do a manual test using a canary release.
Here's my plan:

  • I'm going to take the repro: https://stackblitz.com/edit/github-szu539pq?file=vite.config.ts
  • Upgrade/switch it to my canary
  • Ensure the .env files exist
  • Run npm run build (executes vite build --mode=uat)
  • Check if the built JS (in /dist/client/assets/index-*.js) contains the wrong environment variable (VITE_ENV_EXAMPLE=PRODUCTION instead of VITE_ENV_EXAMPLE=UAT)

🦋 Canary release

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

More information
Published version 0.0.0-pr-33469-sha-4021d8de
Triggered by @ndelangen
Repository storybookjs/storybook
Branch norbert/improve-vitest-addon-plugin
Commit 4021d8de
Datetime Tue Jan 6 10:32:21 UTC 2026 (1767695541)
Workflow run 20745609634

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=33469

Summary by CodeRabbit

  • Improvements
    • Enhanced Vitest plugin initialization to better handle environment configuration and build mode setup.
    • Streamlined configuration resolution for improved Vitest integration.

✏️ Tip: You can customize this high-level summary in your review settings.

…itest "mode" the plugin becomes a no-op.

This is important, because users can configure their vite config in such a way that this addon is evaluated during every vite command, not just vitest/test
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 6, 2026

📝 Walkthrough

Walkthrough

Vitest plugin initialization now returns an empty plugin array when the VITEST env flag is falsy. Env resolution is deferred into the plugin config hook via presets.apply('env', {}); BUILD_TARGET is set from the plugin mode to align with Vitest mode-based env loading. The transform runtime guard was removed. (49 words)

Changes

Cohort / File(s) Summary
Vitest plugin changes
code/addons/vitest/src/vitest-plugin/index.ts
Plugin now returns an empty array early if VITEST is falsy. Removed initial storybookEnv handling and env preset resolution; env is now populated inside the config hook via presets.apply('env', {}). Propagates BUILD_TARGET from plugin mode. Removed runtime VITEST guard in transform. Adjusted destructuring / Promise.all ordering accordingly.

Sequence Diagram(s)

sequenceDiagram
    %% Styling for clarity
    participant CLI as CLI/Env
    participant Presets as Presets
    participant VitestPlugin as Vitest Plugin (index.ts)
    participant VitestCfg as Vitest Config
    participant Transformer as Transform

    rect rgb(240,248,255)
    CLI->>VitestPlugin: load plugin (with env flags, mode)
    alt VITEST falsy
        VitestPlugin-->>CLI: return [] (no plugin)
    else VITEST truthy
        VitestPlugin->>VitestPlugin: enter config hook
        VitestPlugin->>Presets: presets.apply('env', {})
        Presets-->>VitestPlugin: env object
        VitestPlugin->>VitestCfg: set BUILD_TARGET = mode (if present)
        VitestPlugin->>VitestCfg: merge env into config
        VitestCfg-->>VitestPlugin: finalized config
        VitestPlugin->>Transformer: register transform (no VITEST runtime guard)
    end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Build: Upgrade to Vitest 4 #33086 — Related changes touching the same addons/vitest/src/vitest-plugin/index.ts area (env loading, BUILD_TARGET propagation, transform behavior).
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4021d8d and 75ea041.

📒 Files selected for processing (1)
  • code/addons/vitest/src/vitest-plugin/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • code/addons/vitest/src/vitest-plugin/index.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: normal
  • GitHub Check: Core Unit Tests, windows-latest
  • GitHub Check: nx

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

@ndelangen ndelangen self-assigned this Jan 6, 2026
@ndelangen ndelangen added performance issue patch:yes Bugfix & documentation PR that need to be picked to main branch ci:normal addon: vitest labels Jan 6, 2026
@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Jan 6, 2026

View your CI Pipeline Execution ↗ for commit 75ea041

Command Status Duration Result
nx run-many -t compile,check,knip,test,pretty-d... ✅ Succeeded 4m 34s View ↗

☁️ Nx Cloud last updated this comment at 2026-01-06 12:20:42 UTC

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Jan 6, 2026

View your CI Pipeline Execution ↗ for commit 4021d8d


☁️ Nx Cloud last updated this comment at 2026-01-06 10:30:58 UTC

Comment thread code/addons/vitest/src/vitest-plugin/index.ts Outdated
Co-authored-by: Jeppe Reinhold <jeppe@chromatic.com>
@ndelangen ndelangen requested a review from JReinhold January 6, 2026 12:10
@ndelangen ndelangen added the bug label Jan 6, 2026
@ndelangen ndelangen merged commit e5d2edd into next Jan 6, 2026
73 of 74 checks passed
@ndelangen ndelangen deleted the norbert/improve-vitest-addon-plugin branch January 6, 2026 13:00
valentinpalkovic pushed a commit that referenced this pull request Jan 26, 2026
…on-plugin

AddonVitest: Improve perf & fix loading incorrect `.env` file
(cherry picked from commit e5d2edd)
@ndelangen ndelangen removed the patch:yes Bugfix & documentation PR that need to be picked to main branch label Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Vite testing plugin unconditionally loads our .env.production file, breaking builds when also using Cloudflare plugin

2 participants