Skip to content

CSF: Add Storybook test syntax (Storybook v9)#32391

Closed
yannbf wants to merge 159 commits into
latest-releasefrom
feature/test-syntax-in-csf
Closed

CSF: Add Storybook test syntax (Storybook v9)#32391
yannbf wants to merge 159 commits into
latest-releasefrom
feature/test-syntax-in-csf

Conversation

@yannbf
Copy link
Copy Markdown
Member

@yannbf yannbf commented Sep 3, 2025

Closes #32244

What I did

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-32391-sha-41c83af5. Try it out in a new sandbox by running npx storybook@0.0.0-pr-32391-sha-41c83af5 sandbox or in an existing project with npx storybook@0.0.0-pr-32391-sha-41c83af5 upgrade.

More information
Published version 0.0.0-pr-32391-sha-41c83af5
Triggered by @yannbf
Repository storybookjs/storybook
Branch feature/test-syntax-in-csf
Commit 41c83af5
Datetime Thu Sep 4 18:48:24 UTC 2025 (1757011704)
Workflow run 17473700454

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 canary-release-pr.yml --field pr=32391

Greptile Summary

Updated On: 2025-09-03 17:13:50 UTC

This PR introduces a comprehensive test syntax feature for Storybook's Component Story Format (CSF) that allows developers to define test functions directly on stories using a .test() method. The changes implement a new hierarchical story/test relationship where tests become child entries of their parent stories.

Key Changes:

Core Architecture:

  • Extends StoryIndexEntry with a subtype field ('story' | 'test') to distinguish between regular stories and test entries
  • Adds parent and parentName fields for test entries to establish hierarchical relationships
  • Introduces API_TestEntry type alongside existing API_StoryEntry for type safety

Test Syntax API:

  • Stories now support .test(name, testFn) and .test(name, overrides, testFn) method calls
  • Test functions receive the same StoryContext as play functions and can be async or sync
  • Tests inherit parent story's args, parameters, and globals but can override them
  • Tests are automatically tagged with 'test-fn' and '!autodocs' to distinguish them from regular stories

UI Integration:

  • Adds TestNode components to the sidebar with dedicated test icons (beaker icon in green)
  • Stories with tests become expandable branch nodes in the sidebar tree
  • Enhanced tag filtering system with support for tag presets, inverted filtering, and test-specific tags
  • Search results and story navigation updated to handle test subtypes

Indexing and Processing:

  • CSF parser extended to detect .test() method calls and extract test metadata
  • Story indexer generates separate index entries for tests using toTestId() function with colon separator (e.g., storyId:testName)
  • Vitest integration updated to support running individual tests within stories

Type System Updates:

  • All existing story entries systematically updated to include subtype: 'story'
  • New TestFunction type introduced parallel to existing PlayFunction
  • Enhanced type safety across manager API and preview components

The feature enables a new testing paradigm where component tests can be co-located with their stories, inheriting story configuration while providing test-specific overrides. This creates a more integrated testing experience within the Storybook ecosystem.

PR Description Notes:

  • The PR description is incomplete - the "What I did" section is empty and no issue number is provided
  • None of the testing checkboxes are marked, despite this being a major feature addition
  • The manual testing section contains only placeholder text

yannbf and others added 30 commits May 22, 2025 19:50
- Changed instances of `isCI` to `isCI()` in various files to ensure consistent function usage.
- Updated related logic in `postinstall-logger.ts`, `postinstall.ts`, `withTelemetry.ts`, and others to reflect this change.
- Enhanced test coverage for `isCI` behavior in `storybook-metadata.test.ts`.
- Replaced direct imports of `isCI` and `logger` from internal paths with imports from centralized utility modules.
- This change enhances maintainability and consistency across the codebase.
@yannbf yannbf added ci:normal Run our default set of CI jobs (choose this for most PRs). and removed ci:normal Run our default set of CI jobs (choose this for most PRs). labels Sep 5, 2025
@yannbf yannbf changed the title CSF: Add Storybook test syntax CSF: Add Storybook test syntax (Storybook v9) Sep 15, 2025
@github-actions github-actions Bot added the Stale label Sep 29, 2025
@yannbf yannbf closed this Sep 29, 2025
@kasperpeulen kasperpeulen deleted the feature/test-syntax-in-csf branch April 4, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci: do not merge ci:normal Run our default set of CI jobs (choose this for most PRs). csf feature request Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants