Skip to content

Conversation

@mrginglymus
Copy link
Contributor

@mrginglymus mrginglymus commented Jul 3, 2025

Closes #31940

What I did

Sniff for usage of csf factories in vitest.setup and skip transformation

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

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

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-31941-sha-498c84dc. Try it out in a new sandbox by running npx [email protected] sandbox or in an existing project with npx [email protected] upgrade.

More information
Published version 0.0.0-pr-31941-sha-498c84dc
Triggered by @valentinpalkovic
Repository mrginglymus/storybook
Branch addon-a11y-migrate-fix
Commit 498c84dc
Datetime Wed Dec 3 12:03:03 UTC 2025 (1764763383)
Workflow run 19893145747

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

Greptile Summary

Modified the a11y addon migration logic to prevent incorrect transformations of vitest.setup files when CSF factories are detected. This fixes an issue where the standard transformation would fail for projects using Component Story Format factories.

  • Added detection for preview.composed.beforeAll in setup files to identify CSF factory usage
  • Updated addonA11yAddonTest.check() to set skipVitestSetupTransformation=true when factories are detected
  • Added test coverage verifying the detection and handling of CSF factory scenarios
  • Addresses incompatibility issues between the default migration path and CSF factory setups

Summary by CodeRabbit

  • New Features

    • Automigrations now detect CSF 4 usage and tailor fixes accordingly.
    • Improved compatibility with projects using CSF factory previews.
  • Bug Fixes

    • More accurate handling of a11y addon test migration under CSF 4.
    • Avoids unnecessary Vitest setup transformation when CSF 4 is detected.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, 1 comment
Edit PR Review Bot Settings | Greptile

@nx-cloud
Copy link

nx-cloud bot commented Jul 3, 2025

View your CI Pipeline Execution ↗ for commit 2ffb1e1

Command Status Duration Result
nx run-many -t compile --parallel=3 ✅ Succeeded 46s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-03 15:37:30 UTC

@storybook-app-bot
Copy link

storybook-app-bot bot commented Jul 7, 2025

Package Benchmarks

Commit: 2ffb1e1, ran on 3 December 2025 at 15:28:37 UTC

No significant changes detected, all good. 👏

@Sidnioulz
Copy link
Member

@mrginglymus could you please let me know how feasible it'd be to use an AST-based approach to detect preview? We have a few examples using Babel's AST tools in other migrations.

Also, in #32083, I set up a test bed with the few scenarios/edge cases to handle spread over branches. This immensely speeds up testing and review, as reviewers otherwise have to build a sandbox from scratch for every iteration of the PR. It would be lovely (but not required :) ) if you could set one up too.

@mrginglymus
Copy link
Contributor Author

@Sidnioulz I've pushed the AST approach - there was already a utility for csf4 sniffing so I've used that in the project gathering step and propagated it to all migrations.

That does mean we have no automated tests now because the migration tests only test code after the gather phase; I'll need to sniff around for tests that capture an earlier phase - or, as you say, set up a repro.

@Sidnioulz
Copy link
Member

That does mean we have no automated tests now because the migration tests only test code after the gather phase; I'll need to sniff around for tests that capture an earlier phase - or, as you say, set up a repro.

I feel that should be acceptable. You could use the SB logger and add debug messages in your migration code, or even assertions that would fail when your script finds an illogical/incoherent situation.

If your script fails when it can't produce a coherent output, then users know when to switch to manual migration. Part of the deal is we need to point to the right documentation resource for them.

Besides, if we have an E2E test somewhere, then everything works at the time the script is most needed. It might break later (as NPM deps evolve), and at the moment, we don't run any repro case in CI for any automigration. It might be something we'd want to automate, but the ROI isn't as clear or high as other things the team is working on, so it hasn't happened yet.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 6, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

Adds a new boolean csf4 flag computed from preview config, threads it through project collection and automigration pipelines, updates types and data structures, and uses it in the addon-a11y check to gate vitest setup transformation behavior.

Changes

Cohort / File(s) Summary
CSF4 detection in config
code/lib/cli-storybook/src/automigrate/helpers/mainConfigFile.ts
Imports isCsfFactoryPreview; reads preview config to compute csf4; returns csf4 from getStorybookData.
Project collection and result plumbing
code/lib/cli-storybook/src/util.ts
Extends CollectProjectsSuccessResult with csf4; captures csf4 from getStorybookData and includes it in process/collect results.
Automigration data and execution flow
code/lib/cli-storybook/src/automigrate/multi-project.ts
Adds csf4 to ProjectAutomigrationData; passes csf4 into checks and run options for fixes.
Check options typing
code/lib/cli-storybook/src/automigrate/types.ts
Adds csf4: boolean to CheckOptions.
A11y addon test fix behavior
code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.ts
Updates check signature to accept csf4; uses csf4 to set skipVitestSetupTransformation and branch logic.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as CLI User
  participant CLI as CLI Automigrate
  participant Util as util.processProject
  participant CFG as getStorybookData
  participant CSF as csf-tools

  Dev->>CLI: run automigrate
  CLI->>Util: processProject(projectPath)
  Util->>CFG: getStorybookData(configDir)
  CFG->>CFG: locate previewConfigPath
  alt previewConfigPath found
    CFG->>CSF: readConfig(previewPath)
    CSF-->>CFG: previewConfig
    CFG->>CSF: isCsfFactoryPreview(previewConfig)
    CSF-->>CFG: csf4:boolean
  else
    CFG->>CFG: csf4 = false
  end
  CFG-->>Util: { ..., csf4 }
  Util-->>CLI: CollectProjectsSuccessResult { ..., csf4 }

  note over CLI: Automigration phase
  CLI->>CLI: build ProjectAutomigrationData with csf4
  CLI->>Fixes: check(options{ ..., csf4 })
  alt addon-a11y-addon-test
    Fixes->>Fixes: skipVitestSetupTransformation = csf4
  end
  CLI->>Fixes: run(options{ ..., csf4 })
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

@Sidnioulz
Copy link
Member

@mrginglymus I've updated your branch to trigger a fresh Windows test build as I think it was outdated, but FYI there are TS errors showing up in the CircleCI jobs, and you have failing tests for your automigration too.

See https://app.circleci.com/pipelines/github/storybookjs/storybook/104084/workflows/b6438da6-cd7b-4215-8684-e8bcefb1901f

Copy link
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: 0

🧹 Nitpick comments (1)
code/lib/cli-storybook/src/automigrate/helpers/mainConfigFile.ts (1)

182-183: Clarify the log message and add error handling.

Two concerns:

  1. The log message 'Getting csf version...' is misleading—it's actually detecting CSF factory usage in the preview file, not retrieving a version number. Consider changing to 'Detecting CSF factory usage...' for clarity.

  2. If readConfig(previewConfigPath) throws (e.g., due to syntax errors in the preview file), it will crash the entire data collection. Consider wrapping in try-catch:

  logger.debug('Detecting CSF factory usage...');
- const csf4 = previewConfigPath ? isCsfFactoryPreview(await readConfig(previewConfigPath)) : false;
+ let csf4 = false;
+ if (previewConfigPath) {
+   try {
+     csf4 = isCsfFactoryPreview(await readConfig(previewConfigPath));
+   } catch (err) {
+     logger.debug(`Failed to read preview config for CSF factory detection: ${err}`);
+   }
+ }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 38b157d and deae6a6.

📒 Files selected for processing (5)
  • code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.ts (2 hunks)
  • code/lib/cli-storybook/src/automigrate/helpers/mainConfigFile.ts (3 hunks)
  • code/lib/cli-storybook/src/automigrate/multi-project.ts (3 hunks)
  • code/lib/cli-storybook/src/automigrate/types.ts (1 hunks)
  • code/lib/cli-storybook/src/util.ts (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Adhere to ESLint and Prettier rules across all JS/TS source files

Files:

  • code/lib/cli-storybook/src/automigrate/types.ts
  • code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.ts
  • code/lib/cli-storybook/src/automigrate/multi-project.ts
  • code/lib/cli-storybook/src/util.ts
  • code/lib/cli-storybook/src/automigrate/helpers/mainConfigFile.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Fix type errors and prefer precise typings instead of using any or suppressions, consistent with strict mode

Files:

  • code/lib/cli-storybook/src/automigrate/types.ts
  • code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.ts
  • code/lib/cli-storybook/src/automigrate/multi-project.ts
  • code/lib/cli-storybook/src/util.ts
  • code/lib/cli-storybook/src/automigrate/helpers/mainConfigFile.ts
🧬 Code graph analysis (1)
code/lib/cli-storybook/src/automigrate/helpers/mainConfigFile.ts (1)
code/core/src/csf-tools/ConfigFile.ts (2)
  • isCsfFactoryPreview (1189-1204)
  • readConfig (1175-1178)
⏰ 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). (2)
  • GitHub Check: normal
  • GitHub Check: Core Unit Tests, windows-latest
🔇 Additional comments (3)
code/lib/cli-storybook/src/automigrate/fixes/addon-a11y-addon-test.ts (2)

48-48: LGTM! csf4 parameter correctly added to check signature.

This enables the migration to detect CSF factory usage and adjust behavior accordingly.


74-74: LGTM! Correctly skips vitest setup transformation when CSF factories are detected.

Initializing skipVitestSetupTransformation with csf4 ensures that projects using CSF factories (which use preview.composed.beforeAll pattern) will skip the vitest setup transformation, addressing the issue described in #31940.

code/lib/cli-storybook/src/automigrate/helpers/mainConfigFile.ts (1)

194-194: LGTM! csf4 correctly added to return value.

The csf4 field is properly computed and propagated through the automigration pipeline.

@github-actions github-actions bot added the Stale label Oct 17, 2025
@mrginglymus mrginglymus removed the Stale label Nov 27, 2025
@mrginglymus mrginglymus force-pushed the addon-a11y-migrate-fix branch from cb0c21b to 2fff816 Compare November 27, 2025 11:28
@mrginglymus mrginglymus requested a review from Sidnioulz December 3, 2025 08:08
@mrginglymus mrginglymus force-pushed the addon-a11y-migrate-fix branch from 678ac26 to e36148b Compare December 3, 2025 08:10
@Sidnioulz
Copy link
Member

You didn't provide manual testing instructions, but I trust you have tested against a real use case @mrginglymus :)

@Sidnioulz Sidnioulz changed the title fix: addon-a11y-addon-test migration - do not transform vitest.setup file if using csf factories CLI: Skip vitest transform for CSF Factories in a11y-addon-test automigration Dec 3, 2025
@mrginglymus
Copy link
Contributor Author

Actually I could do with a canary for this one...

@Sidnioulz
Copy link
Member

Actually I could do with a canary for this one...

Valentin triggered the canary creation, it should appear in your PR description shortly!

@mrginglymus
Copy link
Contributor Author

Sad trombone, it doesn't work.

@Sidnioulz Sidnioulz self-requested a review December 3, 2025 14:58
@Sidnioulz
Copy link
Member

Sad trombone, it doesn't work.

Do you have leads to investigate, or would you need some time with the team to talk it out?

@mrginglymus
Copy link
Contributor Author

Sad trombone, it doesn't work.

Do you have leads to investigate, or would you need some time with the team to talk it out?

Simple logic bug, but the test case wasn't realistic enough to capture it. Fixed now.

@valentinpalkovic valentinpalkovic merged commit d28dc81 into storybookjs:next Dec 9, 2025
58 of 59 checks passed
@github-actions github-actions bot mentioned this pull request Dec 9, 2025
13 tasks
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]: addon-a11y-addon-test migration fails if using csf factories

6 participants