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

Upgrade Storybook to 8.x (next) #2373

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 6 additions & 44 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import {resolve, dirname, join} from "path";
import {mergeConfig} from "vite";
import turbosnap from "vite-plugin-turbosnap";
import type {StorybookConfig} from "@storybook/react-vite";

const config: StorybookConfig = {
Expand All @@ -9,52 +6,17 @@ const config: StorybookConfig = {
"../__docs__/**/*.mdx",
],
addons: [
getAbsolutePath("@storybook/addon-essentials"),
getAbsolutePath("@storybook/addon-a11y"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Storybook upgrades always want to drop this back in. I finally searched when it's needed. Apparently in Yarn PnP situations module resolution can fail without this. Good to know we can remove this in Perseus too then.

https://storybook.js.org/docs/faq#how-do-i-fix-module-resolution-in-special-environments

Copy link
Member Author

Choose a reason for hiding this comment

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

oh I see, thanks for the context.

getAbsolutePath("@storybook/addon-designs"),
getAbsolutePath("@storybook/addon-interactions"),
getAbsolutePath("@storybook/addon-mdx-gfm"),
getAbsolutePath("storybook-addon-pseudo-states"),
"@storybook/addon-essentials",
"@storybook/addon-a11y",
"@storybook/addon-designs",
"storybook-addon-pseudo-states",
"@storybook/experimental-addon-test",
],
staticDirs: ["../static"],
core: {
disableTelemetry: true,
},
framework: getAbsolutePath("@storybook/react-vite"),
async viteFinal(config, {configType}) {
// Merge custom configuration into the default config
Copy link
Member Author

Choose a reason for hiding this comment

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

note: I'm moving this to a separate vite.config.ts file for better integration with vitest.

Choose a reason for hiding this comment

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

y'all are switching to vitest?

Choose a reason for hiding this comment

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

I see, it's just for running storybook tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

correct! I'm thinking that we should probably write an ADR if we want to adopt this more widely.

Just curious.... I know this is a big change, but I wonder if you have any opinions about switching from Jest to vitest for unit tests?

const mergedConfig = mergeConfig(config, {
resolve: {
// Allow us to detect changes from local wonder-blocks packages.
alias: [
{
find: /^@khanacademy\/wonder-blocks(-.*)$/,
replacement: resolve(
__dirname,
"../packages/wonder-blocks$1/src",
),
},
],
},
});

// Add turbosnap to production builds so we can let Chromatic take
// snapshots only to stories associated with the current PR.
if (configType === "PRODUCTION") {
config.plugins?.push(
turbosnap({rootDir: config.root || process.cwd()}),
);
}

Comment on lines -43 to -47
Copy link
Member Author

Choose a reason for hiding this comment

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

note: Dropping this as it is no longer needed as a separate package. SB 8 now includes it internally.

return mergedConfig;
},
docs: {
autodocs: true,
},
framework: "@storybook/react-vite",
};

export default config;

function getAbsolutePath(value: string): any {
return dirname(require.resolve(join(value, "package.json")));
}
48 changes: 25 additions & 23 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from "react";
import wonderBlocksTheme from "./wonder-blocks-theme";

// import wonderBlocksTheme from "./wonder-blocks-theme";
import {Decorator} 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.

Totally out of the blue and unrelated to this PR, but it looks like you don't have import/order enabled in Wonder Blocks. I (not sure about others Perseus) really love how it automatically orders imports so I don't think about it... although, maybe you don't think about it either. :D

https://github.com/Khan/perseus/blob/main/.eslintrc.js#L220..L236

Copy link
Member Author

Choose a reason for hiding this comment

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

Great point! I'll add this rule in a separate PR. I'll probably wait until WB is in a more "quiet" state to apply the changes :)

import {color} from "@khanacademy/wonder-blocks-tokens";
import Link from "@khanacademy/wonder-blocks-link";
import {ThemeSwitcherContext} from "@khanacademy/wonder-blocks-theming";
Expand Down Expand Up @@ -84,7 +84,7 @@ const parameters = {
ignoreSelector:
".docs-story h2, .docs-story h3, .sbdocs #stories, .sbdocs #usage, .sbdocs-subtitle",
},
theme: wonderBlocksTheme,
// theme: wonderBlocksTheme,
components: {
// Override the default link component to use the WB Link component.
a: Link,
Expand All @@ -95,31 +95,31 @@ const parameters = {
},
};

export const decorators = [
(Story, context) => {
const theme = context.globals.theme;
const enableRenderStateRootDecorator =
context.parameters.enableRenderStateRootDecorator;

if (enableRenderStateRootDecorator) {
return (
<RenderStateRoot>
<ThemeSwitcherContext.Provider value={theme}>
<Story />
</ThemeSwitcherContext.Provider>
</RenderStateRoot>
);
}
const withThemeSwitcher: Decorator = (
Story,
{globals: {theme}, parameters: {enableRenderStateRootDecorator}},
) => {
if (enableRenderStateRootDecorator) {
return (
<ThemeSwitcherContext.Provider value={theme}>
<Story />
</ThemeSwitcherContext.Provider>
<RenderStateRoot>
<ThemeSwitcherContext.Provider value={theme}>
<Story />
</ThemeSwitcherContext.Provider>
</RenderStateRoot>
);
},
];
}
return (
<ThemeSwitcherContext.Provider value={theme}>
<Story />
</ThemeSwitcherContext.Provider>
);
};

export const decorators = [withThemeSwitcher];

const preview: Preview = {
parameters,

globalTypes: {
// Allow the user to select a theme from the toolbar.
theme: {
Expand All @@ -146,6 +146,8 @@ const preview: Preview = {
},
},
},

tags: ["autodocs"],
};

export default preview;
9 changes: 9 additions & 0 deletions .storybook/vitest.setup.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

note: This file was generated when I added the experimental addon. More info here: https://storybook.js.org/docs/writing-tests/test-addon#automatic-setup

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { beforeAll } from 'vitest';
import { setProjectAnnotations } from '@storybook/react';
import * as projectAnnotations from './preview';

// This is an important step to apply the right configuration when testing your stories.
// More info at: https://storybook.js.org/docs/api/portable-stories/portable-stories-vitest#setprojectannotations
const project = setProjectAnnotations([projectAnnotations]);

beforeAll(project.beforeAll);
28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@
"@khanacademy/eslint-plugin": "^2.0.0",
"@khanacademy/wonder-stuff-testing": "^3.0.1",
"@rollup/plugin-node-resolve": "^15.0.2",
"@storybook/addon-a11y": "^8.2.1",
"@storybook/addon-actions": "^8.2.1",
"@storybook/addon-designs": "^8.0.3",
"@storybook/addon-docs": "^8.2.1",
"@storybook/addon-essentials": "^8.2.1",
"@storybook/addon-interactions": "^8.2.1",
"@storybook/addon-mdx-gfm": "^8.2.1",
"@storybook/preview-api": "^8.2.1",
"@storybook/react": "^8.2.1",
"@storybook/react-vite": "^8.2.1",
"@storybook/test": "^8.2.1",
"@storybook/addon-a11y": "^8.5.0-alpha.10",
Copy link
Contributor

Choose a reason for hiding this comment

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

-alpha.10 😱

Are you planning to wait till this is released before landing? 😁

Copy link
Member Author

Choose a reason for hiding this comment

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

hehe living on the edge! j/k

I can totally wait until it is officially released :). I mainly wanted to start this PR to be able to unlock their upcoming Accessibility addon (that uses this new testing tooling), so @marcysutton can try it out and give us some feedback, and also give feedback to the Storybook team as well, which she's planning to do :).

"@storybook/addon-actions": "^8.5.0-alpha.10",
"@storybook/addon-designs": "^8.0.4",
"@storybook/addon-docs": "^8.5.0-alpha.10",
"@storybook/addon-essentials": "^8.5.0-alpha.10",
"@storybook/experimental-addon-test": "^8.5.0-alpha.10",
"@storybook/preview-api": "^8.5.0-alpha.10",
"@storybook/react": "^8.5.0-alpha.10",
"@storybook/react-vite": "^8.5.0-alpha.10",
"@storybook/test": "^8.5.0-alpha.10",
"@swc-node/register": "^1.6.5",
"@swc/core": "^1.3.36",
"@testing-library/jest-dom": "^5.16.5",
Expand All @@ -80,6 +79,8 @@
"@types/react-window": "^1.8.5",
"@typescript-eslint/eslint-plugin": "^5.59.5",
"@typescript-eslint/parser": "^5.59.5",
"@vitejs/plugin-react": "^4.3.3",
"@vitest/browser": "^2.1.5",
"alex": "^11.0.0",
"babel-jest": "^29.7.0",
"babel-loader": "^8.2.3",
Expand Down Expand Up @@ -109,17 +110,18 @@
"jest-extended": "^4.0.2",
"jscodeshift": "^0.15.1",
"npm-package-json-lint": "^6.4.0",
"playwright": "^1.49.0",
"prettier": "^2.8.1",
"react-refresh": "^0.14.0",
"rollup": "^2.79.1",
"rollup-plugin-babel": "^4.0.0-beta.2",
"rollup-plugin-node-externals": "^7.1.2",
"storybook": "^8.2.1",
"storybook": "^8.5.0-alpha.10",
"storybook-addon-pseudo-states": "^3.1.1",
"typescript": "^4.9.5",
"typescript-coverage-report": "^0.7.0",
"vite": "^4.4.8",
"vite-plugin-turbosnap": "^1.0.2"
"vitest": "^2.1.5"
},
"dependencies": {
"@babel/runtime": "^7.18.6",
Expand Down
25 changes: 25 additions & 0 deletions vite.config.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

note: Moved this to a separate config file so it can be reused by Storybook and vitest.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {resolve} from "path";
import react from "@vitejs/plugin-react";
import {defineConfig} from "vite";

export default defineConfig({
plugins: [react()],
resolve: {
alias: [
// Allow us to detect changes from local wonder-blocks packages.
{
find: /^@khanacademy\/wonder-blocks(-.*)$/,
replacement: resolve(
__dirname,
"./packages/wonder-blocks$1/src",
),
},
// Needed for Storybook + React to work with vitest.
// https://github.com/storybookjs/storybook/issues/26842
{
find: "@storybook/react-dom-shim",
replacement: "@storybook/react-dom-shim/dist/react-16",
},
],
},
});
23 changes: 23 additions & 0 deletions vitest.config.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

note: This file was generated when I added the experimental addon. More info here: https://storybook.js.org/docs/writing-tests/test-addon#automatic-setup

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {mergeConfig} from "vitest/config";
import {storybookTest} from "@storybook/experimental-addon-test/vitest-plugin";

Check failure on line 2 in vitest.config.ts

View workflow job for this annotation

GitHub Actions / Lint / Lint (ubuntu-latest, 20.x)

Cannot find module '@storybook/experimental-addon-test/vitest-plugin' or its corresponding type declarations.
import viteConfig from "./vite.config";

// More info at: https://storybook.js.org/docs/writing-tests/vitest-plugin
export default mergeConfig(viteConfig, {
plugins: [
// See options at: https://storybook.js.org/docs/writing-tests/vitest-plugin#storybooktest
storybookTest({configDir: ".storybook"}),
],
test: {
name: "storybook",
browser: {
enabled: true,
headless: true,
name: "chromium",
provider: "playwright",
},
// Make sure to adjust this pattern to match your stories files.
include: ["./__docs__/**/*.stories.@(ts|tsx)"],
setupFiles: [".storybook/vitest.setup.ts"],
},
});
Loading
Loading