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 { expect, fn, userEvent, within } from '@storybook/test';
import { Button } from './Button';
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.4.0
Storybook 8.4 comes with a ton of exciting new features designed to give you the best experience developing, testing, and debugging tests in the browser!
- ▶️ Unified UI for component testing
- 5️⃣ Svelte 5 and Svelte CSF support
- ⚛️ React Native Storybook 8 release
- 🏷️ Tags-based filtering to organize your Storybook
- 🫧 Dependency cleanup to reduce install footprint
- 💯 Hundreds more improvements
<details>
<summary>List of all updates</summary>
- Addon Test: Adjust file exports to be ESM/CJS compatible - #29471, thanks @valentinpalkovic!
- Addon Test: Error when addon interactions exists - #29434, thanks @valentinpalkovic!
- Addon Test: Escape XML when converting ANSI to HTML in test errors - #29446, thanks @ghengeveld!
- Addon Test: Fix hiding stacktrace for assertion errors in test panel - #29458, thanks @ghengeveld!
- Addon Test: Improve Error Handling - #29476, thanks @valentinpalkovic!
- Addon Test: Improve postinstall script - #29479, thanks @yannbf!
- Addon Test: Improve unsupported vitest message - #29486, thanks @valentinpalkovic!
- Addon Test: Only register testing module in Vite projects - #29472, thanks @yannbf!
- Addon Test: Throttle Vitest progress updates more heavily - #29482, thanks @ghengeveld!
- Addon-docs, blocks: Prebundle dependencies - #29301, thanks @JReinhold!
- Addon-Test: Support for
@vitest/browser
v2.1.2 - #29407, thanks @strozw! - Blocks: Prebundle
es-toolkit
- #29259, thanks @JReinhold! - Builder-vite: Replace .at() call with [] in codegen - #29048, thanks @Chudesnov!
- CLI: Don't add
@storybook/addon-links
by default - #29177, thanks @tobiasdiez! - CLI: Ensure
.gitignore
updated via CLI ends with a newline - #29124, thanks @3w36zj6! - CLI: Fix
yarn
detection - #29448, thanks @ndelangen! - CLI: Migrate from
chalk
to picocolors
- #28262, thanks @43081j! - CLI: Refactor NPMProxy error parsing logic - #29459, thanks @yannbf!
- ConfigFile: Fix
export { X }
parsing - #29344, thanks @vctqs1! - Core: Add unified UI Testing Module - #29241, thanks @yannbf!
- Core: Close story status menu when selecting an item - #29455, thanks @ghengeveld!
- Core: Fix building Storybook deleting project root files - #29371, thanks @JReinhold!
- Core: Fix race condition during empty folder init - #29490, thanks @valentinpalkovic!
- Core: Make
prettier
an optional peer dependency - #29223, thanks @JReinhold! - Core: Migrate from
express
to polka
- #29230, thanks @43081j! - Core: Migrate from
qs
to picoquery
- #28315, thanks @43081j! - Core: Open 'Component tests' addon panel when clicking a story status - #29456, thanks @ghengeveld!
- Core: Remove
handlebars
usage - #29208, thanks @ndelangen! - Core: Remove dependence on
file-system-cache
- #29256, thanks @ndelangen! - Core: Replace
fs-extra
with the native APIs - #29126, thanks @ziebam! - Core: Replace
lodash
with es-toolkit
- #28981, thanks @ndelangen! - Core: Show checkmark icon in story status dropdown and update status label for component tests - #29451, thanks @ghengeveld!
- Core: Show tooltip on filter toggles to clarify their purpose - #29447, thanks @ghengeveld!
- Core: Track test provider state in sessionStorage - #29450, thanks @ghengeveld!
- Core: Upgrade
esbuild
, broadening version range - #29254, thanks @ndelangen! - Dependencies: Upgrade VTA to v3.1.0 - #29449, thanks @ghengeveld!
- Dependencies: Upgrade VTA to v3.2.0 to resolve peerDep conflict - #29461, thanks @ghengeveld!
- Interactions: Escape xml of interactions errors - #29414, thanks @kasperpeulen!
- Maintenance: Fix broken and outdated documentation links - #29412, thanks @jonniebigodes!
- Manager: Add tags property to ComponentEntry objects - #29343, thanks @Sidnioulz!
- React: Prebundle all of
renderers/react
's dependencies - #29298, thanks @ndelangen! - Svelte: Improve argTypes inference with
svelte2tsx
- support runes - #29423, thanks @JReinhold! - Test: Remove unused
util
dependency - #29310, thanks @JReinhold! - UI: Brand image css class conflict causes image to resize on hot reloads - #29129, thanks @ShreySinha02!
- UI: Fix RefIndicator to use CheckIcon instead of string - #29209, thanks @JSMike!
- UI: Simple tag filtering - #29333, thanks @shilman!
- UI: Use production-mode
react
in manager - #29197, thanks @ndelangen! - Viewport-addon: Add InitialViewportKeys type to viewport addon - #29182, thanks @hyeongrok7874!
- Vite: Add jsdoc-type-pratt-parser to
optimizeDeps
- #29179, thanks @tobiasdiez! - Vite: Cleanup and prebundle dependencies - #29302, thanks @JReinhold!
- Webpack: Fix export 'act' (imported as 'React4') was not found in 'react' errors in webpack - #29235, thanks @kasperpeulen!
</details>