Skip to content

Commit 02f0f5b

Browse files
authored
Merge pull request storybookjs#31457 from storybookjs/yann/ignore-mdx-in-vitest
Addon Vitest: Ignore mdx files as part of tests
2 parents 880d77f + e5aeba7 commit 02f0f5b

4 files changed

Lines changed: 60 additions & 7 deletions

File tree

code/addons/docs/src/preset.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export const addons: PresetProperty<'addons'> = [
157157

158158
export const viteFinal = async (config: any, options: Options) => {
159159
const { plugins = [] } = config;
160+
160161
const { mdxPlugin } = await import('./plugins/mdx-plugin');
161162

162163
// Use the resolvedReact preset to alias react and react-dom to either the users version or the version shipped with addon-docs
@@ -186,7 +187,10 @@ export const viteFinal = async (config: any, options: Options) => {
186187
// mdx plugin needs to be before any react plugins
187188
plugins.unshift(mdxPlugin(options));
188189

189-
return config;
190+
return {
191+
...config,
192+
plugins,
193+
};
190194
};
191195

192196
/*

code/addons/vitest/src/vitest-plugin/index.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const defaultOptions: UserOptions = {
4848
storybookScript: undefined,
4949
configDir: resolve(join(WORKING_DIR, '.storybook')),
5050
storybookUrl: 'http://localhost:6006',
51+
disableAddonDocs: true,
5152
};
5253

5354
const extractTagsFromPreview = async (configDir: string) => {
@@ -79,6 +80,27 @@ const getStoryGlobsAndFiles = async (
7980
};
8081
};
8182

83+
/**
84+
* Plugin to stub MDX imports during testing This prevents the need to process MDX files in the test
85+
* environment
86+
*/
87+
const mdxStubPlugin: Plugin = {
88+
name: 'storybook:stub-mdx-plugin',
89+
enforce: 'pre',
90+
resolveId(id) {
91+
if (id.endsWith('.mdx')) {
92+
return id;
93+
}
94+
return null;
95+
},
96+
load(id) {
97+
if (id.endsWith('.mdx')) {
98+
return `export default {};`;
99+
}
100+
return null;
101+
},
102+
};
103+
82104
const PACKAGE_DIR = dirname(require.resolve('@storybook/addon-vitest/package.json'));
83105

84106
export const storybookTest = async (options?: UserOptions): Promise<Plugin[]> => {
@@ -129,16 +151,23 @@ export const storybookTest = async (options?: UserOptions): Promise<Plugin[]> =>
129151
extractTagsFromPreview(finalOptions.configDir),
130152
]);
131153

132-
// filter out plugins that we know are unnecesary for tests, eg. docgen plugins
133-
const plugins = (await withoutVitePlugins(viteConfigFromStorybook.plugins ?? [], [
134-
'storybook:package-deduplication', // addon-docs
135-
'storybook:mdx-plugin', // addon-docs
154+
const pluginsToIgnore = [
136155
'storybook:react-docgen-plugin',
137156
'vite:react-docgen-typescript', // aka @joshwooding/vite-plugin-react-docgen-typescript
138157
'storybook:svelte-docgen-plugin',
139158
'storybook:vue-component-meta-plugin',
140-
'storybook:vue-docgen-plugin',
141-
])) as unknown as Plugin[];
159+
];
160+
161+
if (finalOptions.disableAddonDocs) {
162+
pluginsToIgnore.push('storybook:package-deduplication', 'storybook:mdx-plugin');
163+
}
164+
165+
// filter out plugins that we know are unnecesary for tests, eg. docgen plugins
166+
const plugins = await withoutVitePlugins(viteConfigFromStorybook.plugins ?? [], pluginsToIgnore);
167+
168+
if (finalOptions.disableAddonDocs) {
169+
plugins.push(mdxStubPlugin);
170+
}
142171

143172
const storybookTestPlugin: Plugin = {
144173
name: 'vite-plugin-storybook-test',

code/addons/vitest/src/vitest-plugin/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ export type UserOptions = {
2727
exclude?: string[];
2828
skip?: string[];
2929
};
30+
31+
/**
32+
* Whether to disable addon docs features while running tests.
33+
*
34+
* Most users don't need addon docs for tests, the only scenario where you might need it is if you
35+
* need to read and parse MDX files as part of rendering your components.
36+
*
37+
* @default true
38+
*/
39+
disableAddonDocs?: boolean;
3040
};
3141

3242
export type InternalOptions = Required<UserOptions> & {

docs/writing-tests/vitest-addon.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,14 @@ Default:
468468
- **`exclude`**: Stories with these tags will not be tested, and will not be counted in the test results
469469
- **`skip`**: Stories with these tags will not be tested, and will be counted in the test results
470470

471+
#### `disableAddonDocs`
472+
473+
Type: `boolean`
474+
475+
Default: `true`
476+
477+
Whether to disable addon docs MDX parsing while running tests.
478+
479+
When either the preview config or stories import mdx files, they are mocked as normally they are not needed for tests.
480+
You might set `disableAddonDocs` to `false` only in case your stories actually need to read and parse MDX files as part of rendering your components.
471481
</If>

0 commit comments

Comments
 (0)