Skip to content

Commit

Permalink
Merge pull request #28924 from storybookjs/docs-addon-vitest
Browse files Browse the repository at this point in the history
Docs: Vitest plugin
  • Loading branch information
kylegach committed Sep 9, 2024
2 parents 011f77d + de82bf0 commit dbf8ea1
Show file tree
Hide file tree
Showing 18 changed files with 802 additions and 18 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions docs/_snippets/addon-test-install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```shell renderer="common" language="js" packageManager="npx"
npx storybook add @storybook/experimental-addon-test
```

```shell renderer="common" language="js" packageManager="pnpm"
pnpm exec storybook add @storybook/experimental-addon-test
```

```shell renderer="common" language="js" packageManager="yarn"
yarn exec storybook add @storybook/experimental-addon-test
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```tsx filename="setupTest.ts" renderer="react" language="ts"
import { beforeAll } from 'vitest';
// 👇 If you're using Next.js, import from @storybook/nextjs
// If you're using Next.js with Vite, import from @storybook/experimental-nextjs-vite
import { setProjectAnnotations } from '@storybook/react';
import * as previewAnnotations from './.storybook/preview';

const annotations = setProjectAnnotations([previewAnnotations]);

// Run Storybook's beforeAll hook
beforeAll(annotations.beforeAll);
```

```tsx filename="setupTest.ts" renderer="svelte" language="ts"
import { beforeAll } from 'vitest';
// 👇 If you're using Sveltekit, import from @storybook/sveltekit
import { setProjectAnnotations } from '@storybook/svelte';
import * as previewAnnotations from './.storybook/preview';

const annotations = setProjectAnnotations([previewAnnotations]);

// Run Storybook's beforeAll hook
beforeAll(annotations.beforeAll);
```

```tsx filename="setupTest.ts" renderer="vue" language="ts"
import { beforeAll } from 'vitest';
import { setProjectAnnotations } from '@storybook/vue3';
import * as previewAnnotations from './.storybook/preview';

const annotations = setProjectAnnotations([previewAnnotations]);

// Run Storybook's beforeAll hook
beforeAll(annotations.beforeAll);
```
18 changes: 6 additions & 12 deletions docs/_snippets/portable-stories-vitest-set-project-annotations.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
```tsx filename="setupTest.ts" renderer="react" language="ts"
import { beforeAll } from 'vitest';
// 👇 If you're using Next.js, import from @storybook/nextjs
// If you're using Next.js with Vite, import from @storybook/experimental-nextjs-vite
import { setProjectAnnotations } from '@storybook/react';
// 👇 Import the exported annotations, if any, from the addons you're using; otherwise remove this
import * as addonAnnotations from 'my-addon/preview';
import * as previewAnnotations from './.storybook/preview';

const annotations = setProjectAnnotations([
previewAnnotations,
addonAnnotations
]);
const annotations = setProjectAnnotations([previewAnnotations, addonAnnotations]);

// Run Storybook's beforeAll hook
beforeAll(annotations.beforeAll);
```

```tsx filename="setupTest.ts" renderer="svelte" language="ts"
import { beforeAll } from 'vitest';
// 👇 If you're using Sveltekit, import from @storybook/sveltekit
import { setProjectAnnotations } from '@storybook/svelte';
// 👇 Import the exported annotations, if any, from the addons you're using; otherwise remove this
import * as addonAnnotations from 'my-addon/preview';
import * as previewAnnotations from './.storybook/preview';

const annotations = setProjectAnnotations([
previewAnnotations,
addonAnnotations
]);
const annotations = setProjectAnnotations([previewAnnotations, addonAnnotations]);

// Run Storybook's beforeAll hook
beforeAll(annotations.beforeAll);
Expand All @@ -37,10 +34,7 @@ import { setProjectAnnotations } from '@storybook/vue3';
import * as addonAnnotations from 'my-addon/preview';
import * as previewAnnotations from './.storybook/preview';

const annotations = setProjectAnnotations([
previewAnnotations,
addonAnnotations
]);
const annotations = setProjectAnnotations([previewAnnotations, addonAnnotations]);

// Run Storybook's beforeAll hook
beforeAll(annotations.beforeAll);
Expand Down
11 changes: 11 additions & 0 deletions docs/_snippets/vitest-plugin-run-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```shell renderer="common" language="js" packageManager="npm"
npm run test
```

```shell renderer="common" language="js" packageManager="pnpm"
pnpm run test
```

```shell renderer="common" language="js" packageManager="yarn"
yarn test
```
29 changes: 29 additions & 0 deletions docs/_snippets/vitest-plugin-vitest-config-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
```js filename=".storybook/main.js" renderer="common" tabTitle="Before"
import { mergeConfig } from 'vite';

export default {
// ...
viteFinal: async (viteConfig) => {
return mergeConfig(viteConfig, {
resolve: {
alias: {
'@components': '/src/components',
// ...
},
},
});
},
};
```

```js filename="vitest.config.ts" renderer="common" tabTitle="After"
export default defineConfig({
// ...
resolve: {
alias: {
'@components': '/src/components',
// ...
},
},
});
```
118 changes: 118 additions & 0 deletions docs/_snippets/vitest-plugin-vitest-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
```ts filename="vitest.config.ts" renderer="react"
import { defineConfig, mergeConfig } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-test/vitest-plugin';
// 👇 If you're using Next.js, apply this framework plugin as well
// import { storybookNextjsPlugin } from '@storybook/experimental-nextjs-vite/vite-plugin';

import viteConfig from './vite.config';

export default mergeConfig(
viteConfig,
defineConfig({
plugins: [
storybookTest({
// This should match your package.json script to run Storybook
// The --ci flag will skip prompts and not open a browser
storybookScript: 'yarn storybook --ci',
}),
// storybookNextjsPlugin(),
],
test: {
// Glob pattern to find story files
include: ['src/**/*.stories.?(m)[jt]s?(x)'],
// Enable browser mode
browser: {
enabled: true,
name: 'chromium',
// Make sure to install Playwright
provider: 'playwright',
headless: true,
},
// Speed up tests and better match how they run in Storybook itself
// https://vitest.dev/config/#isolate
// Consider removing this if you have flaky tests
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
})
);
```

```ts filename="vitest.config.ts" renderer="vue"
import { defineConfig, mergeConfig } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-test/vitest-plugin';
import { storybookVuePlugin } from '@storybook/vue3-vite/vite-plugin';

import viteConfig from './vite.config';

export default mergeConfig(
viteConfig,
defineConfig({
plugins: [
storybookTest({
// This should match your package.json script to run Storybook
// The --ci flag will skip prompts and not open a browser
storybookScript: 'yarn storybook --ci',
}),
storybookVuePlugin(),
],
test: {
// Glob pattern to find story files
include: ['src/**/*.stories.?(m)[jt]s?(x)'],
// Enable browser mode
browser: {
enabled: true,
name: 'chromium',
// Make sure to install Playwright
provider: 'playwright',
headless: true,
},
// Speed up tests and better match how they run in Storybook itself
// https://vitest.dev/config/#isolate
// Consider removing this if you have flaky tests
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
})
);
```

```ts filename="vitest.config.ts" renderer="svelte"
import { defineConfig, mergeConfig } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-test/vitest-plugin';
// 👇 If you're using Sveltekit, apply this framework plugin as well
// import { storybookNextjsPlugin } from '@storybook/sveltekit/vite-plugin';

import viteConfig from './vite.config';

export default mergeConfig(
viteConfig,
defineConfig({
plugins: [
storybookTest({
// This should match your package.json script to run Storybook
// The --ci flag will skip prompts and not open a browser
storybookScript: 'yarn storybook --ci',
}),
// storybookSveltekitPlugin(),
],
test: {
// Glob pattern to find story files
include: ['src/**/*.stories.?(m)[jt]s?(x)'],
// Enable browser mode
browser: {
enabled: true,
name: 'chromium',
// Make sure to install Playwright
provider: 'playwright',
headless: true,
},
// Speed up tests and better match how they run in Storybook itself
// https://vitest.dev/config/#isolate
// Consider removing this if you have flaky tests
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
})
);
```
128 changes: 128 additions & 0 deletions docs/_snippets/vitest-plugin-vitest-workspace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
```ts title="vitest.workspace.ts" renderer="react"
import { defineWorkspace } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-test/vitest-plugin';
// 👇 If you're using Next.js, apply this framework plugin as well
// import { storybookNextjsPlugin } from '@storybook/experimental-nextjs-vite/vite-plugin';

export default defineWorkspace([
// This is the path to your existing Vitest config file
'./vitest.config.ts',
{
name: 'storybook',
// This is the path to your existing Vite config file
extends: './vite.config.ts',
plugins: [
storybookTest({
// This should match your package.json script to run Storybook
// The --ci flag will skip prompts and not open a browser
storybookScript: 'yarn storybook --ci',
}),
// storybookNextjsPlugin(),
],
test: {
// Glob pattern to find story files
include: ['src/**/*.stories.?(m)[jt]s?(x)'],
// Enable browser mode
browser: {
enabled: true,
name: 'chromium',
// Make sure to install Playwright
provider: 'playwright',
headless: true,
},
// Speed up tests and better match how they run in Storybook itself
// https://vitest.dev/config/#isolate
// Consider removing this if you have flaky tests
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
},
]);
```

```ts filename="vitest.config.ts" renderer="vue"
import { defineConfig, mergeConfig } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-test/vitest-plugin';
import { storybookVuePlugin } from '@storybook/vue3-vite/vite-plugin';

import viteConfig from './vite.config';

export default defineWorkspace([
// This is the path to your existing Vitest config file
'./vitest.config.ts',
{
name: 'storybook',
// This is the path to your existing Vite config file
extends: './vite.config.ts',
plugins: [
storybookTest({
// This should match your package.json script to run Storybook
// The --ci flag will skip prompts and not open a browser
storybookScript: 'yarn storybook --ci',
}),
storybookVuePlugin(),
],
test: {
// Glob pattern to find story files
include: ['src/**/*.stories.?(m)[jt]s?(x)'],
// Enable browser mode
browser: {
enabled: true,
name: 'chromium',
// Make sure to install Playwright
provider: 'playwright',
headless: true,
},
// Speed up tests and better match how they run in Storybook itself
// https://vitest.dev/config/#isolate
// Consider removing this if you have flaky tests
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
},
]);
```

```ts filename="vitest.config.ts" renderer="svelte"
import { defineConfig, mergeConfig } from 'vitest/config';
import { storybookTest } from '@storybook/experimental-addon-test/vitest-plugin';
// 👇 If you're using Sveltekit, apply this framework plugin as well
// import { storybookNextjsPlugin } from '@storybook/sveltekit/vite-plugin';

import viteConfig from './vite.config';

export default defineWorkspace([
// This is the path to your existing Vitest config file
'./vitest.config.ts',
{
name: 'storybook',
// This is the path to your existing Vite config file
extends: './vite.config.ts',
plugins: [
storybookTest({
// This should match your package.json script to run Storybook
// The --ci flag will skip prompts and not open a browser
storybookScript: 'yarn storybook --ci',
}),
// storybookSveltekitPlugin(),
],
test: {
// Glob pattern to find story files
include: ['src/**/*.stories.?(m)[jt]s?(x)'],
// Enable browser mode
browser: {
enabled: true,
name: 'chromium',
// Make sure to install Playwright
provider: 'playwright',
headless: true,
},
// Speed up tests and better match how they run in Storybook itself
// https://vitest.dev/config/#isolate
// Consider removing this if you have flaky tests
isolate: false,
setupFiles: ['./.storybook/vitest.setup.ts'],
},
},
]);
```
2 changes: 1 addition & 1 deletion docs/api/portable-stories/portable-stories-jest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: 'Portable stories in Jest'
sidebar:
title: Jest
order: 1
order: 2
---

<If notRenderer={['react', 'vue']}>
Expand Down
Loading

0 comments on commit dbf8ea1

Please sign in to comment.