Skip to content

Commit 5efca7a

Browse files
authored
Merge pull request #36396 from storybookjs/ari/sb-1993
Addon Vitest: Prevent users from calling `setProjectAnnotations`, add auto migration for `vitest.setup.ts`
2 parents f0fad60 + 9a5a88a commit 5efca7a

27 files changed

Lines changed: 2631 additions & 805 deletions

File tree

‎MIGRATION.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Top-level `setConfig` layout and UI options removed](#top-level-setconfig-layout-and-ui-options-removed)
1111
- [Sidebar label rendering: renderAriaLabel and a context argument](#sidebar-label-rendering-renderarialabel-and-a-context-argument)
1212
- [Vitest Addon: requires Vitest 4.0 or higher](#vitest-addon-requires-vitest-40-or-higher)
13+
- [Vitest Addon: `setProjectAnnotations` must not be called in setup files](#vitest-addon-setprojectannotations-must-not-be-called-in-setup-files)
1314
- [Vite: `publicDir` is handled by Storybook's `staticDirs`](#vite-publicdir-is-handled-by-storybooks-staticdirs)
1415
- [Vite: requires Vite 6.3 or higher](#vite-requires-vite-63-or-higher)
1516
- [Next.js: Require v15 and up](#nextjs-require-v15-and-up)
@@ -677,6 +678,10 @@ The `@storybook/addon-vitest` addon requires **Vitest 4.0 or higher**. Setup now
677678

678679
If your custom tooling imports `canUpdateVitestWorkspaceFile` from `storybook/internal/babel`, remove that import. The workspace-file helper has been removed; migrate the configuration to `test.projects`.
679680

681+
### Vitest Addon: `setProjectAnnotations` must not be called in setup files
682+
683+
TODO
684+
680685
### Vite: `publicDir` is handled by Storybook's `staticDirs`
681686

682687
In previous versions, Vite copied its `publicDir` (`public/` by default) into the output of `storybook build` after Storybook had written its own files. A `public/index.json` silently replaced Storybook's story index and broke the built Storybook, and files from `public/` overrode files from your `staticDirs`.

‎agent-eval/evals/812-first-story-empty-project/.storybook/vitest.setup.ts‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎agent-eval/evals/812-first-story-empty-project/vitest.storybook.config.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export default defineConfig({
2828
provider: playwright({}),
2929
instances: [{ browser: 'chromium' }],
3030
},
31-
setupFiles: ['.storybook/vitest.setup.ts'],
3231
},
3332
},
3433
],

‎agent-eval/templates/monorepo/packages/ui/.storybook/vitest.setup.ts‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎agent-eval/templates/monorepo/packages/ui/vitest.storybook.config.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export default defineConfig({
2828
provider: playwright({}),
2929
instances: [{ browser: 'chromium' }],
3030
},
31-
setupFiles: ['.storybook/vitest.setup.ts'],
3231
},
3332
},
3433
],

‎agent-eval/templates/reshaped-storybook/.storybook/vitest.setup.ts‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎agent-eval/templates/reshaped-storybook/vitest.storybook.config.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export default defineConfig({
3636
provider: playwright({}),
3737
instances: [{ browser: 'chromium' }],
3838
},
39-
setupFiles: ['.storybook/vitest.setup.ts'],
4039
},
4140
},
4241
],

‎code/.storybook/ensure-channel.ts‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎code/.storybook/preview.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { toHaveLiveRegion } from '../core/src/shared/utils/toHaveLiveRegion.ts';
3434
import * as templatePreview from '../core/template/stories/preview.ts';
3535
import '../renderers/react/template/components/index.js';
3636
import { isChromatic } from './isChromatic.ts';
37+
import * as vitestPreview from './vitest.preview.ts';
3738

3839
sb.mock(import('@storybook/global'), { spy: true });
3940

@@ -432,6 +433,7 @@ export default definePreview({
432433
addonTest(),
433434
addonPseudoStates(),
434435
templatePreview,
436+
...((import.meta as { env?: { VITEST?: unknown } }).env?.VITEST ? [vitestPreview] : []),
435437
],
436438
decorators,
437439
loaders,

‎code/.storybook/storybook.setup.ts‎

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
1-
import { vi, expect as vitestExpect } from 'vitest';
2-
3-
import { setProjectAnnotations } from '@storybook/react';
4-
5-
import { userEvent as storybookEvent, expect as storybookExpect } from 'storybook/test';
1+
import { vi } from 'vitest';
62

73
import '../core/src/shared/utils/toHaveLiveRegion.ts';
84

9-
// Must be imported before `./preview.tsx` so a channel exists when preview open-services register.
10-
import './ensure-channel.ts';
11-
12-
import preview from './preview.tsx';
13-
145
vi.spyOn(console, 'warn').mockImplementation((...args) => console.log(...args));
15-
16-
setProjectAnnotations([
17-
preview.composed,
18-
{
19-
// experiment with injecting Vitest's interactivity API over our userEvent while tests run in browser mode
20-
// https://vitest.dev/guide/browser/interactivity-api.html
21-
loaders: async (context) => {
22-
if (globalThis.__vitest_browser__) {
23-
const vitest = await import('vitest/browser');
24-
const { userEvent: browserEvent } = vitest;
25-
// Unfortunately the types of userEvent don't match so we cast it
26-
context.userEvent = (browserEvent as unknown as typeof storybookEvent).setup();
27-
context.expect = vitestExpect;
28-
} else {
29-
context.userEvent = storybookEvent.setup();
30-
context.expect = storybookExpect;
31-
}
32-
},
33-
},
34-
]);

0 commit comments

Comments
 (0)