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 Vitest: Improve transformation logic to avoid duplicate tests #28929

Merged
merged 5 commits into from
Aug 20, 2024

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Aug 20, 2024

Closes #28930

This work changes the transformation done in the plugin to:

  • account for no tests in a file
  • transform only the stories that should be included (uses tags at compile time)
  • execute only tests if they are imported in the same

This fixes several bugs:

1 - test fails because there are no tests in the file

export default {
  tags: ['!test']
}

// ...

2 - test from another file execute when being imported from a story file

import { someConstant } from './Other.stories' // this causes unintended side effects
export default {}
export const MyStory = {}

Before:
image

After:
image

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/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 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 76.3 MB 76.3 MB -106 B -0.45 0%
initSize 169 MB 169 MB 3.37 kB 1.74 0%
diffSize 92.8 MB 92.8 MB 3.47 kB 1.27 0%
buildSize 7.46 MB 7.46 MB 138 B 1.19 0%
buildSbAddonsSize 1.62 MB 1.62 MB 138 B 1.33 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 2.3 MB 2.3 MB 0 B 0.33 0%
buildSbPreviewSize 351 kB 351 kB 0 B 1.09 0%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 4.46 MB 4.46 MB 138 B 1.23 0%
buildPreviewSize 3 MB 3 MB 0 B 1.02 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 6.7s 20.4s 13.6s 0.65 66.9%
generateTime 20s 18.2s -1s -842ms -1.81 🔰-10.1%
initTime 16.5s 15.9s -598ms -1.18 -3.8%
buildTime 13s 14.4s 1.4s 1.46 🔺9.9%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 7.8s 7.8s -30ms 0.44 -0.4%
devManagerResponsive 5s 5.1s 119ms 0.65 2.3%
devManagerHeaderVisible 903ms 855ms -48ms 0.39 -5.6%
devManagerIndexVisible 943ms 893ms -50ms 0.36 -5.6%
devStoryVisibleUncached 1.2s 1.4s 214ms 0.63 14.4%
devStoryVisible 940ms 894ms -46ms 0.37 -5.1%
devAutodocsVisible 695ms 891ms 196ms 1.29 🔺22%
devMDXVisible 697ms 719ms 22ms -0.06 3.1%
buildManagerHeaderVisible 733ms 781ms 48ms 0.33 6.1%
buildManagerIndexVisible 739ms 791ms 52ms 0.39 6.6%
buildStoryVisible 807ms 879ms 72ms 0.58 8.2%
buildAutodocsVisible 718ms 810ms 92ms 1.13 11.4%
buildMDXVisible 633ms 726ms 93ms 1 12.8%

Greptile Summary

This PR enhances the Vitest addon for Storybook, focusing on improving the transformation logic and test execution efficiency. Key changes include:

  • Implemented filtering of stories based on tags at compile time in code/core/src/csf-tools/vitest-plugin/transformer.ts
  • Added extraction of preview-level tags in code/addons/vitest/src/plugin/index.ts
  • Improved handling of cases with no valid tests in a file in code/core/src/csf-tools/vitest-plugin/transformer.ts
  • Implemented a guard to prevent duplicate test executions in code/core/src/csf-tools/vitest-plugin/transformer.ts
  • Updated testStory function in code/addons/vitest/src/plugin/test-utils.ts to use composeStory for more accurate story composition

This work changes the transformation done in the plugin to:
- account for no tests in a file
- transform only the stories that should be included (uses tags at compile time)
- execute only tests if they are imported in the same
@yannbf yannbf added bug ci:daily Run the CI jobs that normally run in the daily job. addon: test labels Aug 20, 2024
Copy link

nx-cloud bot commented Aug 20, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit fad8af0. 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.

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, 1 comment(s)
Edit PR Review Bot Settings

return async (context: TestContext & TaskContext & { story: ComposedStoryFn }) => {
if (Story === undefined || tagsFilter?.skip.some((tag) => Story.tags.includes(tag))) {
if (composedStory === undefined || skipTags?.some((tag) => composedStory.tags.includes(tag))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Check if composedStory is undefined before accessing its properties

@yannbf yannbf enabled auto-merge August 20, 2024 12:42
@yannbf yannbf added the ci:merged Run the CI jobs that normally run when merged. label Aug 20, 2024
@yannbf yannbf removed the ci:daily Run the CI jobs that normally run in the daily job. label Aug 20, 2024
@yannbf yannbf merged commit a57a1ab into next Aug 20, 2024
68 checks passed
@yannbf yannbf deleted the yann/addon-vitest-transformation-fixes branch August 20, 2024 16:12
@github-actions github-actions bot mentioned this pull request Aug 20, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addon: test bug ci:merged Run the CI jobs that normally run when merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix tests becoming side effect when importing from other story files
2 participants