[Bug]: Svelte CSF: `mount()` in `play()` fails with `SB_PREVIEW_API_0012 (MountMustBeDestructuredError)`
Describe the bug
In a Svelte CSF story (.stories.svelte), a play function that destructures mount fails with this error:
SB_PREVIEW_API_0012 (MountMustBeDestructuredError): Incorrect use of mount in the play function.The story renders before play starts, so the mount() call throws. The docs describe this pattern here: https://storybook.js.org/docs/writing-tests/component-testing#run-code-before-the-component-gets-rendered
Storybook core decides if a story uses mount from the source text of the story's play function. Svelte CSF does not give core the user's play. It gives core a wrapper, (storyContext) => { … }, which runs the real play from the component scope (create-runtime-stories.ts). Core never sees { mount } in the wrapper, so it does not wait for play to call mount().
The error shows the wrapper:
Received the following play function:
(storyContext) => {
var _a;
const delegate = (_a = storyContext.playFunction) == null ? void 0 : _a.__play;
if (delegate) {
return delegate(storyContext);
}
return play(storyContext);
}Reproduction link
No link. The reproduction is one story file, see the steps below.
Reproduction steps
Add this story to a Svelte project with
@storybook/addon-svelte-csf:<script module> import { defineMeta } from '@storybook/addon-svelte-csf'; import Button from './Button.svelte'; const { Story } = defineMeta({ component: Button, }); </script> <Story name="With Mount" play={async ({ mount }) => { await mount(); }} />Open the story in Storybook, or run it with
@storybook/addon-vitest.The story fails with
SB_PREVIEW_API_0012 (MountMustBeDestructuredError).
Expected: Storybook waits for play to call mount(), and the story renders then.
System
@storybook/addon-svelte-csf: 5.1.4
storybook: 10.3.2
@storybook/svelte-vite: 10.3.2
svelte: 5.50.3
vite: 8.0.1
@sveltejs/vite-plugin-svelte: 7.0.0Additional context
- Still reproducible on 2026-09-28 with the versions above.
- storybookjs/addon-svelte-csf#271 tried to fix this. It made the wrapper destructure
mountwhen the user'splaydoes. The author found that core's check failed again after Vite's dependency cache was warm, and the PR was closed without a merge. - Workaround (from the original issue): use
beforeEachindefineMetaand checkctx.nameto run code before a specific story renders.
Source: storybookjs/storybook