Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon Test: Refactor test addon to include stories automatically #29367

Merged
merged 12 commits into from
Dec 4, 2024

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Oct 16, 2024

Closes #29771

What I did

This PR introduces a change in the Vitest plugin where it leverages the stories field of main.ts to transform into an array of files for vitest, and adds that automatically as config.test.include.

I tested this locally in the monorepo's Storybook and it worked without issues. Adding extra include or exclude paths in the vitest config will work as expected.

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 is tested by our sandboxes, as well as via our storybook:vitest npm script; Which is run in CI.

You can experiment by adding a new story to the monorepo storybook, and running storybook:vitest and check if the new story is tested.

🦋 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/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.7 MB 77.7 MB 0 B 0.68 0%
initSize 130 MB 130 MB 4.88 kB 0.3 0%
diffSize 52.4 MB 52.4 MB 4.88 kB 0.21 0%
buildSize 6.75 MB 6.75 MB 108 B -1.36 0%
buildSbAddonsSize 1.51 MB 1.51 MB 0 B - 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.86 MB 1.86 MB 108 B 1 0%
buildSbPreviewSize 0 B 0 B 0 B -1.36 -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.56 MB 3.57 MB 108 B -1.36 0%
buildPreviewSize 3.19 MB 3.19 MB 0 B 1.36 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 23.9s 6.8s -17s -94ms -1.06 -249.9%
generateTime 19.2s 21.1s 1.9s 0.34 9.1%
initTime 12.8s 14.2s 1.3s 0.29 9.6%
buildTime 9.9s 10.5s 541ms 1.21 5.1%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 5.7s 4.2s -1s -467ms -1.56 🔰-34.7%
devManagerResponsive 4.2s 3.2s -1s -13ms -1.26 🔰-31.5%
devManagerHeaderVisible 655ms 504ms -151ms -1.06 -30%
devManagerIndexVisible 726ms 568ms -158ms -1.02 -27.8%
devStoryVisibleUncached 2s 1.7s -351ms -0.28 -20.4%
devStoryVisible 727ms 532ms -195ms -1.25 🔰-36.7%
devAutodocsVisible 605ms 489ms -116ms -0.69 -23.7%
devMDXVisible 643ms 444ms -199ms -0.93 -44.8%
buildManagerHeaderVisible 674ms 494ms -180ms -0.9 -36.4%
buildManagerIndexVisible 792ms 569ms -223ms -0.61 -39.2%
buildStoryVisible 617ms 460ms -157ms -1.09 -34.1%
buildAutodocsVisible 566ms 422ms -144ms -0.68 -34.1%
buildMDXVisible 520ms 380ms -140ms -1.16 -36.8%

Greptile Summary

Based on the provided files and context, here's a concise summary of the changes:

Refactored the Storybook test addon to automatically sync story discovery between Storybook and Vitest testing.

  • Removed hardcoded story glob patterns from Vitest config in favor of using Storybook's stories field from main.js
  • Added StoryIndexGenerator export from core-server for consistent story file discovery
  • Added automatic MDX file exclusion and framework-specific plugin configurations for Next.js, SvelteKit, and Vue3
  • Fixed setup file path resolution in workspace configuration for better monorepo support
  • Added framework-specific plugin imports and configurations to handle different framework requirements

The changes aim to resolve inconsistencies where stories might be visible in Storybook but not in Vitest tests or vice versa, while maintaining compatibility with different frameworks and build configurations.

Copy link

nx-cloud bot commented Oct 16, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 5c9c675. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@storybook-pr-benchmarking
Copy link

storybook-pr-benchmarking bot commented Dec 3, 2024

Package Benchmarks

Commit: 5c9c675, ran on 4 December 2024 at 12:09:14 UTC

The following packages have significant changes to their size or dependencies:

@storybook/experimental-addon-test

Before After Difference
Dependency count 61 61 0
Self size 695 KB 916 KB 🚨 +221 KB 🚨
Dependency size 13.86 MB 13.86 MB 0 B
Bundle Size Analyzer Link Link

@ndelangen ndelangen marked this pull request as ready for review December 3, 2024 16:06
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.

5 file(s) reviewed, 6 comment(s)
Edit PR Review Bot Settings | Greptile

code/.storybook/vitest.config.ts Outdated Show resolved Hide resolved
code/addons/test/src/vitest-plugin/index.ts Outdated Show resolved Hide resolved
code/addons/test/src/vitest-plugin/index.ts Show resolved Hide resolved
code/addons/test/src/vitest-plugin/index.ts Show resolved Hide resolved
@@ -6,3 +6,4 @@ export * from './build-static';
export * from './build-dev';
export * from './withTelemetry';
export { default as build } from './standalone';
export { StoryIndexGenerator } from './utils/StoryIndexGenerator';
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Exposing internal utilities like StoryIndexGenerator could make it harder to refactor internals in the future. Consider creating a dedicated public API for story file discovery instead.

Comment on lines +505 to 506
${template.expected.renderer === '@storybook/svelte' ? '"**/*.stories.svelte",' : ''}
],
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Svelte stories exclusion should be conditional on both renderer AND framework being Svelte to handle SvelteKit case

@ndelangen ndelangen self-assigned this Dec 4, 2024
@ndelangen ndelangen added ci:daily Run the CI jobs that normally run in the daily job. and removed ci:normal labels Dec 4, 2024
code/addons/test/src/postinstall.ts Outdated Show resolved Hide resolved
code/addons/test/src/postinstall.ts Outdated Show resolved Hide resolved
code/addons/test/src/vitest-plugin/index.ts Show resolved Hide resolved
@ndelangen ndelangen merged commit fd02aa5 into next Dec 4, 2024
112 checks passed
@ndelangen ndelangen deleted the yann/automatic-stories-glob branch December 4, 2024 13:02
@github-actions github-actions bot mentioned this pull request Dec 4, 2024
11 tasks
@JReinhold
Copy link
Contributor

@ghengeveld @ndelangen I wonder if our new way of finding stories here, also fixes #29572. We should try it out in QA.

@yannbf
Copy link
Member Author

yannbf commented Dec 6, 2024

@ghengeveld @ndelangen I wonder if our new way of finding stories here, also fixes #29572. We should try it out in QA.

It doesn't seem to fix it. Here's an example on windows with two directories: my stories and my tests with a simple Vitest file. Vitest is able to find and test the my tests one, but fails in my stories:

strangely enough if I use the newest alpha and I keep the include which was there originally (the default one with .stories.), then the tests fail because they end up finding the template stories in node_modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addon: test bug ci:daily Run the CI jobs that normally run in the daily job.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] some stories are visible in the index but not in Vitest, or vice versa
5 participants