Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions code/addons/vitest/src/vitest-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,17 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin[]> =>
const storybookTestPlugin: Plugin = {
name: 'vite-plugin-storybook-test',
async transformIndexHtml(html) {
const [headHtmlSnippet, bodyHtmlSnippet] = await Promise.all([
const [frameworkOptions, headHtmlSnippet, bodyHtmlSnippet] = await Promise.all([
presets.apply('frameworkOptions'),
presets.apply('previewHead'),
presets.apply('previewBody'),
]);

return html
.replace('</head>', `${headHtmlSnippet ?? ''}</head>`)
.replace(
'</head>',
`<script>window.FRAMEWORK_OPTIONS = ${JSON.stringify(frameworkOptions)};</script>${headHtmlSnippet ?? ''}</head>`
Comment thread
valentinpalkovic marked this conversation as resolved.
)
.replace('<body>', `<body>${bodyHtmlSnippet ?? ''}`);
},
async config(nonMutableInputConfig, { mode }) {
Expand Down
24 changes: 24 additions & 0 deletions code/addons/vitest/src/vitest-plugin/vitest-root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ describe('story test patterns', () => {
});
});

describe('preview html', () => {
it('sets the framework options global ahead of the preview head', async () => {
const applyPreset = presetApply.getMockImplementation()!;
presetApply.mockImplementation(async (key: string, fallback?: unknown) => {
switch (key) {
case 'frameworkOptions':
return { strictMode: true };
case 'previewHead':
return '<meta name="preview-head" />';
default:
return applyPreset(key, fallback);
}
});

const plugins = await storybookTest({ configDir: CONFIG_DIR });
const plugin = plugins.find((p) => p.name === 'vite-plugin-storybook-test')!;
const transformIndexHtml = plugin.transformIndexHtml as (html: string) => Promise<string>;

expect(await transformIndexHtml('<html><head></head><body></body></html>')).toBe(
'<html><head><script>window.FRAMEWORK_OPTIONS = {"strictMode":true};</script><meta name="preview-head" /></head><body></body></html>'
);
});
});

describe('internal setup files', () => {
it('registers internal setup files', async () => {
const { test, plugin } = await getPluginConfig(REPO_ROOT);
Expand Down
Loading