CSF: Refactor CsfFile to support tests and tags#32385
Merged
Merged
Conversation
…retrieving story tests
|
View your CI Pipeline Execution ↗ for commit 639042c
☁️ Nx Cloud last updated this comment at |
| * !autodocs so the negation does not get passed through, and therefore we need to do it here. | ||
| * Therefore, unfortunately we have to duplicate the logic here. | ||
| */ | ||
| const overrideTags = storyAnnotations?.tags?.includes('test-fn') ? ['!autodocs'] : []; |
Contributor
There was a problem hiding this comment.
style: Consider making 'test-fn' a named constant instead of a magic string to improve maintainability
|
|
||
| const hasTests = Object.keys(validStories).some( | ||
| (exportName) => parsed._storyTests[exportName]?.length > 0 | ||
| (exportName) => parsed.getStoryTests(exportName).length > 0 |
Contributor
There was a problem hiding this comment.
style: Same concern about null safety - ensure getStoryTests() consistently returns an array even when no tests exist.
Suggested change
| (exportName) => parsed.getStoryTests(exportName).length > 0 | |
| (exportName) => parsed.getStoryTests(exportName)?.length > 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #
What I did
This PR refactors the CSF file structure to have a flat list of tests, plus a utility to get story tests.
Additionally, it handles story test tags and fixes an issue where the tests were included in autodocs by default.
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
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 pull request has been released as version
0.0.0-pr-32385-sha-07e99aef. Try it out in a new sandbox by runningnpx storybook@0.0.0-pr-32385-sha-07e99aef sandboxor in an existing project withnpx storybook@0.0.0-pr-32385-sha-07e99aef upgrade.More information
0.0.0-pr-32385-sha-07e99aefyann/fix-test-tags-extraction07e99aef1756903196)To request a new release of this pull request, mention the
@storybookjs/coreteam.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=32385Greptile Summary
Updated On: 2025-09-03 11:49:45 UTC
This PR refactors the CsfFile class to improve API design and support test functionality in Storybook's Component Story Format (CSF). The changes introduce a new
getStoryTests()method to replace direct access to private_storyTestsproperties, improving encapsulation and providing a cleaner API surface. Additionally, the PR adds special handling for stories tagged withtest-fnby automatically applying a!autodocsoverride tag to prevent test stories from appearing in documentation.The refactoring touches three key areas: the CSF file test suite, the story preparation logic in preview-api, and the vitest plugin transformer. The
getStoryTests()method now serves as the public interface for accessing story test data, hiding implementation details behind a proper getter method. This change supports Storybook's evolving test infrastructure where stories can function as both visual components and executable tests.The test-fn tag handling ensures that stories intended for testing frameworks like Vitest are properly excluded from Storybook's documentation while remaining available for test execution. This separation allows developers to write test stories alongside regular stories without cluttering the documentation interface.
PR Description Notes:
Confidence score: 4/5