Storybook Test
The @storybook/test
package contains utilities for testing your stories inside play
functions.
Installation
Install the package by adding the @storybook/test
dev dependency:
npm install -D @storybook/test
pnpm add -D @storybook/test
yarn add -D @storybook/test
Note that this package is not an addon, so you don't have to add it to your main.js/main.ts
file.
Usage
The test package exports instrumented versions of @vitest/spy, @vitest/expect (based on chai), @testing-library/dom and @testing-library/user-event.
The instrumentation makes sure you can debug those methods in the addon-interactions panel.
import { Button } from './Button';
import { within, userEvent, expect, fn } from '@storybook/test';
export default {
component: Button,
args: {
onClick: fn(),
},
};
export const Demo = {
play: async ({ args, canvasElement }) => {
const canvas = within(canvasElement);
await userEvent.click(canvas.getByRole('button'));
await expect(args.onClick).toHaveBeenCalled();
},
};
8.0.0
Storybook 8.0 is here
It brings major improvements to Storybook's feature set for testing and documentation, with strengthened framework support across React, Vue, Angular, web-components, Svelte, and more.
- 🩻 Built-in visual testing
- ⚛️ React Server Component support
- 🎛️ Improved controls for React and Vue projects
- ⚡️ Improved Vite architecture, Vitest testing, and Vite 5 support
- 🧪 2-4x faster Storybooks for testing
- ✨ Refreshed desktop UI
- 📲 Rebuilt mobile UX
- 🙅♀️ No more React requirement in non-React projects
Please checkout our Migration Guide to upgrade from earlier versions of Storybook. To see a comprehensive list of changes that went into 8.0, you can refer to the 8.0 prerelease changelogs.