Skip to content

[Bug]: Addon-Vitest: Add support for existing .storybook/vitest.setup.ts files during postinstall step #33695

@valentinpalkovic

Description

@valentinpalkovic

Describe the bug

The current addon-vitest postinstall/init script is too sensitive to existing files. If it detects an existing .storybook/vitest.setup.ts, the process aborts with an error rather than verifying the configuration and proceeding or skipping gracefully. This creates a friction point for users who are incrementally upgrading or who have manually scaffolded their testing environment.

Proposed Solution: "Smart Skip" Logic
Instead of aborting, the postinstall script should be refactored to be idempotent. It should validate the existing environment and only take action if pieces are missing.

Logic Workflow:

  1. Detect: Check for the existence of .storybook/vitest.setup.ts.
  2. Verify Config: Check if vitest.config.ts (or vite.config.ts) already includes:
  • The storybookTest plugin from @storybook/experimental-addon-test.
  • The reference to the setupFiles array pointing to .storybook/vitest.setup.ts.
  1. Evaluate:
  • If fully configured: Log a success message (e.g., "Vitest for Storybook is already properly configured. Skipping setup.") and exit gracefully.
  • If partially configured: Only apply the missing pieces (e.g., if the setup file exists but the config doesn't point to it, update the config).

Reproduction link

Reproduction steps

No response

System

-

Additional context

No response

Metadata

Metadata

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions