Open kasperpeulen opened 4 months ago
mount
You can execute code before rendering by using the mount
function in the play
method. Note that you must always destructure mount
from the context. If you do not, an error will be thrown.
// React Example
export const MyStory = {
async play({ mount }) {
const canvas = await mount(MyComponent, { props: { label: 'label', disabled: true } });
// Your code here
},
};
// Vue3 Example
export const MyStory = {
async play({ mount }) {
const canvas = await mount(MyComponent, { props: { label: 'label', disabled: true } });
// Your code here
},
};
// Svelte Example
export const MyStory = {
async play({ mount }) {
const canvas = await mount(MyComponent, { props: { label: 'label', disabled: true } });
// Your code here
},
};
You can run code before rendering for a single test by using the play
method. This is different from beforeEach
as it runs only for the specific test.
export const MyStory = {
async play({ mount }) {
// Code to run before rendering
const canvas = await mount(MyComponent, { props: { label: 'label', disabled: true } });
// Your code here
},
};
If you do not destructure mount
, an error will be thrown:
export class MountMustBeDestructuredError extends StorybookError {
readonly category = Category.PREVIEW_API;
readonly code = 12;
constructor(public data: { playFunction: string }) {
super();
}
template() {
return dedent`
To use mount in the play function, you must use object destructuring, e.g. play: ({ mount }) => {}.
Instead received:
${this.data.playFunction}
`;
}
}
Mention that stories using mount
need autoplay
in the Story Doc Block:
### `autoplay`
Type: `boolean`
Default: `parameters.docs.story.autoplay`
Determines whether a story's play function runs. Stories using `mount` require `autoplay` to be set to `true`.
/code/core/src/preview-api/modules/preview-web/render/StoryRender.test.ts /code/core/src/preview-errors.ts /code/renderers/vue3/src/public-types.test.ts /code/lib/test/template/stories/mount-in-play.stories.ts /code/renderers/react/src/public-types.test.tsx /code/renderers/svelte/src/public-types.test.ts /docs/writing-stories /docs/api/doc-blocks/doc-block-story.mdx /docs/api/portable-stories /code/core/src/preview-api/modules/preview-web/render