-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Description
Summary
In a monorepo where the Storybook config directory is NOT located at the repository root (e.g. apps/storybook-web/.storybook), @storybook/addon-vitest's storybookTest() plugin fails to discover any test files (Vitest reports no tests found). Setting test.root to the Storybook app directory fixes the issue.
This appears to be a path resolution base (root/cwd) mismatch when the plugin generates/augments Vitest's test include patterns for stories.
Reproduction
Directory Structure:
repo/
package.json
vitest.config.ts
apps/
storybook-web/
.storybook/
main.ts
preview.ts
vitest.setup.ts
src/
Button.tsx
Button.stories.tsx
main.ts (stories relative to configDir as per Storybook docs):
const config: StorybookConfig = {
stories: ['../src/**/*.stories.@(ts|tsx|js|jsx)'],
addons: ['@storybook/addon-vitest'],
framework: { name: '@storybook/react-vite', options: {} },
};vitest.config.ts (Problem - without test.root):
export default defineConfig({
plugins: [
storybookTest({
configDir: path.join(dirname, 'apps/storybook-web/.storybook'),
}),
],
test: {
name: 'storybook',
// NO test.root set
browser: { enabled: true, headless: true, provider: playwright(), instances: [{ browser: 'chromium' }] },
setupFiles: ['./apps/storybook-web/.storybook/vitest.setup.ts'],
},
});Steps to Reproduce
- Place Storybook config under a subdirectory:
apps/storybook-web/.storybook/main.ts - In
main.ts, set stories relative toconfigDir:stories: ['../src/**/*.stories.@(ts|tsx|js|jsx)'] - Add a story file:
apps/storybook-web/src/Button.stories.tsx - Configure Vitest with
storybookTest({ configDir: '<repo>/apps/storybook-web/.storybook' }) - Do NOT set
test.root - Run:
pnpm vitest list -c vitest.config.ts --reporter=verbose
Expected Behavior
Vitest should discover and list/run tests generated from stories, even when configDir is not at repo root.
Actual Behavior
Vitest discovers 0 tests / no output from vitest list.
Output without test.root (Problem):
> vitest list -c vitest.config.ts --reporter=verbose
(no output - no tests found)
Workaround
Setting test.root to the Storybook app directory makes discovery work:
test: {
root: path.join(dirname, 'apps/storybook-web'),
// ...
}Output with test.root (Working):
> vitest list -c vitest.config.with-root.ts --reporter=verbose
[storybook (chromium)] src/Button.stories.tsx > Primary
[storybook (chromium)] src/Button.stories.tsx > Secondary
Environment
- OS: macOS Darwin 24.6.0 (arm64)
- Node: v24.12.0
- pnpm: 10.27.0
- Storybook: 10.2.1
- @storybook/addon-vitest: 10.2.1
- Vitest: 4.0.18
- Vite: 6.4.1
- @vitest/browser: 4.0.18
- Playwright: 1.57.0
Suspected Cause
The storybookTest plugin resolves story globs (from main.ts) against Vitest's root directory instead of configDir. When configDir is nested (e.g., apps/storybook-web/.storybook), the relative glob ../src/**/*.stories.* is interpreted from the wrong base directory.
Potential Fix Directions
- Resolve
main.tsstory globs relative toconfigDirand inject absolute globs into Vitest config - Normalize injected include patterns relative to Vitest root
- Derive an appropriate root (e.g.,
path.dirname(configDir)) internally whenconfigDiris nested - Document the need to set
test.rootwhen using nestedconfigDirin monorepos