CSF4: Fix duplicate preview loading issue in Vitest#34361
Conversation
Co-authored-by: Kasper Peulen <kasperpeulen@gmail.com>
|
View your CI Pipeline Execution ↗ for commit 3ebc4fe
☁️ Nx Cloud last updated this comment at |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughRemoved Storybook preview/config loading and CSF4 detection from the Vitest plugin. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
code/addons/vitest/src/vitest-plugin/utils.ts (1)
41-63:⚠️ Potential issue | 🟠 MajorDon't restrict manual
setProjectAnnotationsdetection toconfigDir.Line 42 is now the only opt-out after removing
isCSF4. A repo-rootvitest.setup.tsor nested.storybook/.../setup.tsthat still callssetProjectAnnotations(...)won't match this exact-parent-dir check, so we'll injectsetup-file-with-project-annotationson top of the user's setup and double-register preview annotations.Possible fix
-import { readFileSync } from 'node:fs'; +import { existsSync, readFileSync } from 'node:fs'; ... -import { dirname, resolve } from 'pathe'; +import { resolve } from 'pathe'; ... const hasStorybookAnnotations = userSetupFiles.find((setupFile) => { - const hasStorybookSetupFileName = dirname(setupFile) === finalOptions.configDir; - - if (!hasStorybookSetupFileName) { + if (!existsSync(setupFile)) { return false; } - // Check if the file contains setProjectAnnotations const setupFileContent = readFileSync(setupFile, 'utf-8'); return setupFileContent.includes('setProjectAnnotations'); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@code/addons/vitest/src/vitest-plugin/utils.ts` around lines 41 - 63, The current detection for manual setProjectAnnotations is restricted by checking dirname(setupFile) === finalOptions.configDir; change the predicate used for hasStorybookAnnotations so it no longer requires the setup file to live directly under finalOptions.configDir: iterate over userSetupFiles and for each file read its contents (readFileSync) and return true if the content includes 'setProjectAnnotations' (remove the hasStorybookSetupFileName check), leaving the existing logBoxOnce and return false behavior intact so any setup file anywhere (e.g., repo-root vitest.setup.ts or nested .storybook/*) will opt out of automatic preview annotation injection.
🧹 Nitpick comments (1)
code/addons/vitest/src/vitest-plugin/index.ts (1)
306-317: Please add a regression test for both provisioning paths.These lines now do two load-bearing things at once: default-enable project annotations and stop injecting the preview file itself. A browser test that proves preview-level
parameters.msw.handlersinheritance still works, plus a legacy case with an explicitsetProjectAnnotationssetup file, would keep both the MSW fix and the duplicate-loading avoidance from regressing.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@code/addons/vitest/src/vitest-plugin/index.ts` around lines 306 - 317, Add two regression tests to cover both provisioning paths introduced around requiresProjectAnnotations/areProjectAnnotationRequired and internalSetupFiles: (1) a browser-style test that mounts a story using preview-level parameters.msw.handlers and asserts the MSW handlers are inherited (verifying we still inject the preview handlers and don't break preview-level inheritance), and (2) a legacy case that supplies an explicit setup file which calls setProjectAnnotations and asserts handlers are applied only once (verifying duplicate-loading avoidance). Place tests alongside the vitest-plugin test suite that exercises index.ts behavior and reference the symbols requiresProjectAnnotations, areProjectAnnotationRequired, internalSetupFiles, and the setup-file paths ('@storybook/addon-vitest/internal/setup-file' and '.../setup-file-with-project-annotations') so they cover both branches.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@code/addons/vitest/src/vitest-plugin/utils.ts`:
- Around line 41-63: The current detection for manual setProjectAnnotations is
restricted by checking dirname(setupFile) === finalOptions.configDir; change the
predicate used for hasStorybookAnnotations so it no longer requires the setup
file to live directly under finalOptions.configDir: iterate over userSetupFiles
and for each file read its contents (readFileSync) and return true if the
content includes 'setProjectAnnotations' (remove the hasStorybookSetupFileName
check), leaving the existing logBoxOnce and return false behavior intact so any
setup file anywhere (e.g., repo-root vitest.setup.ts or nested .storybook/*)
will opt out of automatic preview annotation injection.
---
Nitpick comments:
In `@code/addons/vitest/src/vitest-plugin/index.ts`:
- Around line 306-317: Add two regression tests to cover both provisioning paths
introduced around requiresProjectAnnotations/areProjectAnnotationRequired and
internalSetupFiles: (1) a browser-style test that mounts a story using
preview-level parameters.msw.handlers and asserts the MSW handlers are inherited
(verifying we still inject the preview handlers and don't break preview-level
inheritance), and (2) a legacy case that supplies an explicit setup file which
calls setProjectAnnotations and asserts handlers are applied only once
(verifying duplicate-loading avoidance). Place tests alongside the vitest-plugin
test suite that exercises index.ts behavior and reference the symbols
requiresProjectAnnotations, areProjectAnnotationRequired, internalSetupFiles,
and the setup-file paths ('@storybook/addon-vitest/internal/setup-file' and
'.../setup-file-with-project-annotations') so they cover both branches.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2e71c675-6804-42ae-956c-5d82c3fe8b4a
📒 Files selected for processing (2)
code/addons/vitest/src/vitest-plugin/index.tscode/addons/vitest/src/vitest-plugin/utils.ts
|
CSF4? |
…ssue CSF4: Fix duplicate preview loading issue in Vitest (cherry picked from commit 69c8442)
Closes #34317
What I did
I've fixed a bug where the preview file wasn't deduplicated when users used CSF factories in Vitest tests. The fix actually removes the user's preview.js from the internal setup files and always calls get project annotations, even for CSF factory users. Although setting preview annotations for CSF factory users is obsolete, the internal import of preview annotations via a virtual file will result in deduplication of imports.
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
Caution
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal,ci:mergedorci:dailyGH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/src/sandbox-templates.tsMake 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/coreteam 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