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

Portable stories: Use internal Storybook renderers mechanism #28766

Merged
merged 22 commits into from
Aug 6, 2024

Conversation

kasperpeulen
Copy link
Contributor

@kasperpeulen kasperpeulen commented Jul 31, 2024

Closes #28685

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 0 B -0.03 0%
initSize 198 MB 198 MB -16.1 kB 0.53 0%
diffSize 122 MB 122 MB -16.1 kB 0.78 0%
buildSize 7.62 MB 7.61 MB -11 kB -0.05 -0.1%
buildSbAddonsSize 1.64 MB 1.63 MB -6.91 kB -0.83 -0.4%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 2.31 MB 2.31 MB -461 B 0.47 0%
buildSbPreviewSize 351 kB 349 kB -2.04 kB -1.4 -0.6%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 4.5 MB 4.49 MB -9.41 kB -0.16 -0.2%
buildPreviewSize 3.13 MB 3.13 MB -1.6 kB 0.36 -0.1%
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 23s 24s 1s 2.15 4.2%
generateTime 21.9s 22.5s 606ms 0.26 2.7%
initTime 24.6s 23.4s -1s -176ms 0.61 -5%
buildTime 15.3s 15.8s 413ms 1.8 2.6%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 8.6s 8.6s 28ms -0.29 0.3%
devManagerResponsive 5.5s 5.7s 195ms -0.33 3.4%
devManagerHeaderVisible 787ms 834ms 47ms -0.14 5.6%
devManagerIndexVisible 818ms 867ms 49ms -0.16 5.7%
devStoryVisibleUncached 1.3s 1.3s 3ms -0.66 0.2%
devStoryVisible 839ms 894ms 55ms -0.13 6.2%
devAutodocsVisible 750ms 805ms 55ms 0.16 6.8%
devMDXVisible 720ms 817ms 97ms -0.13 11.9%
buildManagerHeaderVisible 755ms 711ms -44ms -1.76 🔰-6.2%
buildManagerIndexVisible 758ms 716ms -42ms -1.64 🔰-5.9%
buildStoryVisible 804ms 758ms -46ms -1.71 🔰-6.1%
buildAutodocsVisible 709ms 658ms -51ms -1.19 -7.8%
buildMDXVisible 697ms 655ms -42ms -0.74 -6.4%

Greptile Summary

This pull request introduces an internal renderToCanvas method as a fallback when testingLibraryRender is not specified, along with several related changes.

  • code/core/src/preview-api/modules/store/csf/portable-stories.ts: Removed fallback logic for renderToCanvas and introduced a new run function for story execution.
  • code/core/src/preview-errors.ts: Removed TestingLibraryMustBeConfiguredError class to simplify error handling.
  • code/core/src/types/modules/composedStory.ts: Added optional play and mandatory run functions to ComposedStoryFn type.
  • code/lib/react-dom-shim/src/preventActChecks.tsx: Added preventActChecks function to temporarily disable IS_REACT_ACT_ENVIRONMENT flag.
  • code/renderers/react/src/portable-stories.tsx: Introduced conditional check to use internal renderToCanvas method if testingLibraryRender is not specified.

Copy link

nx-cloud bot commented Jul 31, 2024

☁️ Nx Cloud Report

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

@yannbf yannbf changed the title Use internal renderToCanvas method if testingLibaryRender is not specified Portable stories: Use internal Storybook renderers mechanism Jul 31, 2024
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.

27 file(s) reviewed, 25 comment(s)
Edit PR Review Bot Settings

globalProjectAnnotations,
projectAnnotations ?? {},
])
composeConfigs([defaultConfig ?? {}, globalProjectAnnotations, projectAnnotations ?? {}])
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Ensure defaultConfig is correctly populated to avoid unexpected behavior.

@@ -130,7 +113,7 @@ export function composeStory<TRenderer extends Renderer = Renderer, TArgs extend
loaded: {},
abortSignal: new AbortController().signal,
step: (label, play) => story.runStep(label, play, context),
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Setting canvasElement to null! might cause runtime errors if not properly handled.

const context = initializeContext();
context.canvasElement ??= globalThis?.document?.body;
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 globalThis.document.body is always available in all environments.

@@ -325,13 +305,24 @@ export function createPlaywrightTest<TFixture extends { extend: any }>(

// TODO At some point this function should live in prepareStory and become the core of StoryRender.render as well.
// Will make a follow up PR for that
async function playStory<TRenderer extends Renderer>(
async function runStory<TRenderer extends Renderer>(
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Consider extracting the container creation logic to a helper function for better readability.

@@ -235,42 +235,6 @@ export class MountMustBeDestructuredError extends StorybookError {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Ensure the internal renderToCanvas method is robust and can handle all scenarios previously managed by testingLibraryRender.

@@ -1,6 +1,5 @@
```tsx filename="setupTest.ts" renderer="react" language="ts"
import { beforeAll } from '@jest/globals';
import { render as testingLibraryRender } from '@testing-library/react';
import { setProjectAnnotations } from '@storybook/react';
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Ensure that the internal renderToCanvas method provides equivalent functionality to testingLibraryRender.

@@ -9,8 +8,6 @@ import * as previewAnnotations from './.storybook/preview';
const annotations = setProjectAnnotations([
previewAnnotations,
addonAnnotations,
// You MUST provide this option to use portable stories with vitest
{ testingLibraryRender },
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

check: Verify that all necessary annotations are included to avoid missing configurations.

@@ -19,7 +16,6 @@ beforeAll(annotations.beforeAll);

```tsx filename="setupTest.ts" renderer="vue" language="ts"
import { beforeAll } from '@jest/globals';
import { render as testingLibraryRender } from '@testing-library/vue';
import { setProjectAnnotations } from '@storybook/vue3';
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Ensure that the internal renderToCanvas method provides equivalent functionality to testingLibraryRender.

@@ -28,8 +24,6 @@ import * as previewAnnotations from './.storybook/preview';
const annotations = setProjectAnnotations([
previewAnnotations,
addonAnnotations,
// You MUST provide this option to use portable stories with vitest
{ testingLibraryRender },
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

check: Verify that all necessary annotations are included to avoid missing configurations.

Comment on lines 8 to 11
const annotations = setProjectAnnotations([
previewAnnotations,
addonAnnotations,
// You MUST provide this option to use portable stories with vitest
{ testingLibraryRender },
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Ensure that removing testingLibraryRender does not affect existing tests or configurations.

@kasperpeulen kasperpeulen changed the base branch from next to vitest-integration July 31, 2024 14:50
@storybookjs storybookjs deleted a comment from greptile-apps bot Aug 1, 2024
@yannbf yannbf self-assigned this Aug 5, 2024
@@ -152,6 +173,9 @@ exports[`Renders NewStory story 1`] = `
</div>
<!---->
<!---->
<!---->
<!---->
<!---->
Copy link
Member

Choose a reason for hiding this comment

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

any idea on what's going on here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, those are coming from PreviewRender that was first not used in the testing library setup

code/lib/react-dom-shim/src/react-18.tsx Show resolved Hide resolved
);
},
play: async ({ canvasElement, step }) => {
console.log('start of play function');
Copy link
Member

Choose a reason for hiding this comment

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

are these logs needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, I think you added them, I copied it from the test sandbox

Copy link
Member

Choose a reason for hiding this comment

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

feel free to remove them!

@@ -14,7 +14,7 @@ import { setProjectAnnotations, composeStories, composeStory } from '..';
import type { Button } from './Button';
import * as stories from './Button.stories';

setProjectAnnotations([{ testingLibraryRender: render }]);
setProjectAnnotations([]);
Copy link
Member

Choose a reason for hiding this comment

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

This is not needed anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

setProjectAnnotations still register the react annoations. So I need to call it. And TS accepts only accepts an empty array, we probably should change that though?

@yannbf yannbf merged commit 92deb47 into vitest-integration Aug 6, 2024
49 of 51 checks passed
@yannbf yannbf deleted the kasper/render-agnostic branch August 6, 2024 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants