docs/api/portable-stories/portable-stories-vitest.mdx
<If notRenderer={['react', 'vue', 'svelte']}> <Callout variant="info"> Portable stories in Vitest are currently only supported in React, Vue and Svelte projects. </Callout> </If>
<If renderer="svelte"> (⚠️ **Experimental**)<Callout variant="info">
This feature is not supported with the Svelte CSF. To opt-in to this feature with Svelte, you must use Storybook's [Component Story Format](../csf/index.mdx).
<If renderer={['react', 'vue', 'svelte']}> <Callout variant="warning"> Storybook now recommends testing your stories in Vitest with the Vitest addon, which automatically transforms stories into real Vitest tests (using this API under the hood).
This API is still available for those who prefer to use portable stories directly, but we recommend using the Vitest addon for a more streamlined testing experience.
Portable stories are Storybook stories which can be used in external environments, such as Vitest.
Normally, Storybook composes a story and its annotations automatically, as part of the story pipeline. When using stories in Vitest tests, you must handle the story pipeline yourself, which is what the composeStories and composeStory functions enable.
composeStories will process the component's stories you specify, compose each of them with the necessary annotations, and return an object containing the composed stories.
(
csfExports: CSF file exports,
projectAnnotations?: ProjectAnnotations
) => Record<string, ComposedStoryFn>
csfExports(Required)
Type: CSF file exports
Specifies which component's stories you want to compose. Pass the full set of exports from the CSF file (not the default export!). E.g. import * as stories from './Button.stories'
projectAnnotationsType: ProjectAnnotation | ProjectAnnotation[]
Specifies the project annotations to be applied to the composed stories.
This parameter is provided for convenience. You should likely use setProjectAnnotations instead. Details about the ProjectAnnotation type can be found in that function's projectAnnotations parameter.
This parameter can be used to override the project annotations applied via setProjectAnnotations.
Type: Record<string, ComposedStoryFn>
An object where the keys are the names of the stories and the values are the composed stories.
Additionally, the composed story will have the following properties:
| Property | Type | Description |
|---|---|---|
| args | Record<string, any> | The story's args |
| argTypes | ArgType | The story's argTypes |
| id | string | The story's id |
| parameters | Record<string, any> | The story's parameters |
| play | (context) => Promise<void> | undefined | Executes the play function of a given story |
| run | (context) => Promise<void> | undefined | Mounts and executes the play function of a given story |
| storyName | string | The story's name |
| tags | string[] | The story's tags |
You can use composeStory if you wish to compose a single story for a component.
(
story: Story export,
componentAnnotations: Meta,
projectAnnotations?: ProjectAnnotations,
exportsName?: string
) => ComposedStoryFn
story(Required)
Type: Story export
Specifies which story you want to compose.
componentAnnotations(Required)
Type: Meta
The default export from the stories file containing the story.
projectAnnotationsType: ProjectAnnotation | ProjectAnnotation[]
Specifies the project annotations to be applied to the composed story.
This parameter is provided for convenience. You should likely use setProjectAnnotations instead. Details about the ProjectAnnotation type can be found in that function's projectAnnotations parameter.
This parameter can be used to override the project annotations applied via setProjectAnnotations.
exportsNameType: string
You probably don't need this. Because composeStory accepts a single story, it does not have access to the name of that story's export in the file (like composeStories does). If you must ensure unique story names in your tests and you cannot use composeStories, you can pass the name of the story's export here.
Type: ComposedStoryFn
A single composed story.
This API should be called once, before the tests run, typically in a setup file. This will make sure that whenever composeStories or composeStory are called, the project annotations are taken into account as well.
These are the configurations needed in the setup file:
.storybook/preview.tsSometimes a story can require an addon's decorator or loader to render properly. For example, an addon can apply a decorator that wraps your story in the necessary router context. In this case, you must include that addon's preview export in the project annotations set. See addonAnnotations in the example above.
Note: If the addon doesn't automatically apply the decorator or loader itself, but instead exports them for you to apply manually in .storybook/preview.js|ts (e.g. using withThemeFromJSXProvider from @storybook/addon-themes), then you do not need to do anything else. They are already included in the previewAnnotations in the example above.
If you need to configure Testing Library's render or use a different render function, please let us know in this discussion so we can learn more about your needs.
(projectAnnotations: ProjectAnnotation | ProjectAnnotation[]) => ProjectAnnotation
projectAnnotations(Required)
Type: ProjectAnnotation | ProjectAnnotation[]
A set of project annotations (those defined in .storybook/preview.js|ts) or an array of sets of project annotations, which will be applied to all composed stories.
Annotations are the metadata applied to a story, like args, decorators, loaders, and play functions. They can be defined for a specific story, all stories for a component, or all stories in the project.
To preview your stories in Storybook, Storybook runs a story pipeline, which includes applying project annotations, loading data, rendering the story, and playing interactions. This is a simplified version of the pipeline:
When you want to reuse a story in a different environment, however, it's crucial to understand that all these steps make a story. The portable stories API provides you with the mechanism to recreate that story pipeline in your external environment:
Annotations come from the story itself, that story's component, and the project. The project-level annotations are those defined in your .storybook/preview.js file and by addons you're using. In portable stories, these annotations are not applied automatically — you must apply them yourself.
👉 For this, you use the setProjectAnnotations API.
The story is prepared by running composeStories or composeStory. The outcome is a renderable component that represents the render function of the story.
Finally, stories can prepare data they need (e.g. setting up some mocks or fetching data) before rendering by defining loaders, beforeEach or by having all the story code in the play function when using the mount. In portable stories, all of these steps will be executed when you call the run method of the composed story.
👉 For this, you use the composeStories or composeStory API. The composed story will return a run method to be called.
If your stories behave differently based on globals (e.g. rendering text in English or Spanish), you can define those global values in portable stories by overriding project annotations when composing a story:
<CodeSnippets path="portable-stories-vitest-override-globals.md" /> </If>