Skip to content

Addon-Vitest: Support vite/vitest config with deferred export#33755

Merged
valentinpalkovic merged 2 commits into
nextfrom
valentin/support-vitest-config-with-deferred-export
Feb 4, 2026
Merged

Addon-Vitest: Support vite/vitest config with deferred export#33755
valentinpalkovic merged 2 commits into
nextfrom
valentin/support-vitest-config-with-deferred-export

Conversation

@valentinpalkovic
Copy link
Copy Markdown
Contributor

@valentinpalkovic valentinpalkovic commented Feb 3, 2026

Closes #33753

What I did

The Vitest addon’s config updater (updateVitestFile) previously only supported config files that export the config inline, e.g. export default defineConfig({ ... }). It did not support the pattern where the config is assigned to a variable and exported on a separate line:

const config = defineConfig({
  resolve: { ... },
  plugins: [viteReact()],
})
export default config

This pattern is common in setups like TanStack Start and other projects that build the config in multiple steps. For those, the addon’s postinstall never merged in the Vitest/Storybook test configuration.

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

  1. Create or use a project whose Vite/Vitest config uses the variable + export default config pattern (e.g. a TanStack Start app, or a minimal vite.config.ts with const config = defineConfig({...}); export default config).
  2. Add the Vitest addon (e.g. via Storybook init or manually) so that the postinstall/update step runs on the config file.
  3. Confirm the config file is updated: new imports (e.g. path, storybookTest), the dirname helper if needed, and the test block (with projects and the Storybook test config) are merged into the same object that is exported (the one assigned to config).
  4. Run Vitest/Storybook tests to ensure the addon works with the updated config.

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 PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

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

Summary by CodeRabbit

  • New Features

    • Enhanced Vitest configuration update support to handle configurations exported via defineConfig patterns and indirect variable references.
    • Improved merging logic for complex test and workspace configurations.
    • Better compatibility with diverse Vitest configuration export styles.
  • Tests

    • Added test coverage for configuration updates with defineConfig patterns.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 3, 2026

📝 Walkthrough

Walkthrough

Extends the Vitest addon's configuration update logic to handle config objects defined via variable indirection and wrapped in defineConfig. Adds a helper function getTargetConfigObject() to resolve the actual configuration object from various export patterns, along with enhanced merge logic for complex test/workspace scenarios. Includes corresponding test coverage for the defineConfig pattern.

Changes

Cohort / File(s) Summary
Vitest Config Update Implementation
code/addons/vitest/src/updateVitestFile.ts
Introduces getTargetConfigObject() helper to resolve config objects from direct exports, defineConfig wrapping, or variable indirection. Adds rejectFunctionNotation() to detect unsupported function exports. Reworks merge logic to handle more complex source/target shapes, including mergeConfig scenarios, defineConfig patterns, and improved test/workspace/projects configuration handling with coverage propagation.
Test Coverage
code/addons/vitest/src/updateVitestFile.test.ts
Adds new test case verifying updateConfigFile correctly augments Vitest config when defined and exported via defineConfig pattern, ensuring proper injection of imports, dirname constant, comment block, and test configuration entry with projects array, setupFiles, and browser settings.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

✨ Finishing touches
  • 📝 Generate docstrings

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

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Feb 3, 2026

View your CI Pipeline Execution ↗ for commit c633a79

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

☁️ Nx Cloud last updated this comment at 2026-02-04 10:23:42 UTC

@valentinpalkovic valentinpalkovic added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Feb 4, 2026
@valentinpalkovic valentinpalkovic merged commit 23494ee into next Feb 4, 2026
130 of 135 checks passed
@valentinpalkovic valentinpalkovic deleted the valentin/support-vitest-config-with-deferred-export branch February 4, 2026 10:12
valentinpalkovic added a commit that referenced this pull request Feb 4, 2026
…nfig-with-deferred-export

Addon-Vitest: Support vite/vitest config with deferred export
(cherry picked from commit 23494ee)
@github-actions github-actions Bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

addon: vitest bug ci:normal patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Vitest addon cannot update config when default export is not inline

2 participants