Write and compare pieces
A piece is a story you already write, in a story file beside its component.
import { fn } from 'storybook/test';import { Badge } from './Badge';
const meta = { title: 'Pieces/Badge', component: Badge, args: { label: 'New', onSeen: fn() } };export default meta;
export const Primary = {};
export const Warning = { name: 'A warning', args: { label: 'Low stock', tone: 'warning' } };Name the story files in the manifest, with the preview module and the schemes to render.
scenarios: pieces: paths: ['src/**/*.stories.tsx'] preview: .storybook/preview.tsx schemes: [light, dark]A comparison then reads what a screen reader would hear, in each scheme.
$ rehearse compare --base main4 as on the base · 1 changed without saying so · 1 fails on the head (1 on the base too)
changed without saying so: Badge · A warning (src/Badge.stories.tsx) light tree line 2: "- status \"Low stock (warning)\": ⚠ Low stock" → "- status \"Low stock (warning)\": \"Warning: Low stock\"" dark tree line 2: "- status \"Low stock (warning)\": ⚠ Low stock" → "- status \"Low stock (warning)\": \"Warning: Low stock\""A piece is one story of a component, in Storybook’s Component Story Format (CSF). Rehearse reads your story files without running them, renders each story on its own page in your package’s own Vite and React, and observes what a screen reader would hear: the accessibility tree. It also keeps the errors the page logged and the axe violations. If you already write stories, you already write pieces.
Write a piece
Section titled “Write a piece”-
Point the manifest at your stories. List the story files under
scenarios.pieces.rehearse.yaml scenarios:pieces: ['packages/ui/src/**/*.stories.tsx'] -
Write stories beside the component. Every named export is a story, except
defaultand names starting with__. A meta’sincludeStoriesandexcludeStoriesare honoured when they are a list or a regular expression. Rehearse reads the file as text and never runs it to find the stories.Part Comes from id the file and the export: src/Badge.stories.tsx#Warningname the title’s last part and the story’s name(orstoryName, or the export in words):Badge · A warning -
Check the package can render it. Rehearse renders a piece in the nearest folder above the story that has a
package.jsonand can loadvite,reactandreact-dom. It uses that package’s own Vite, with itsvite.config.*(orvitest.config.*) when there is one, and its own React. -
Run it once by its id, with
--jsonto see what it observed.Terminal rehearse run 'src/Badge.stories.tsx#Warning' --jsonThe observation’s
observedpart holds what each scheme rendered.Terminal {"kind": "piece","storyId": "pieces-badge--a-warning","schemes": {"light": {"tree": "- main:\n - status \"Low stock (warning)\": ⚠ Low stock","threw": null,"errors": [],"violations": [],"pixels": "b2e68802bee62a1d"},"dark": {"tree": "- main:\n - status \"Low stock (warning)\": ⚠ Low stock","threw": null,"errors": [],"violations": [],"pixels": "0f0c03655d1cbb81"}}}The screenshots are kept as files in
.rehearse/runs/, listed inartifacts, and shown on the stage. -
Compare.
rehearse compare --base mainrenders each piece on the base and on the head and compares them.
Name the preview module
Section titled “Name the preview module”Stories written for Storybook often need what .storybook/preview gives them: decorators that wrap every story in
providers, global styles, default args. Name the preview module, relative to the piece’s package.
scenarios: pieces: paths: ['src/**/*.stories.tsx'] preview: .storybook/preview.tsxThe following preview wraps every story in a main that carries its tone.
import { useArgs } from 'storybook/preview-api';import './preview.css';
export const decorators = [ (Story: () => unknown) => { const [args] = useArgs(); return ( <main data-tone={String((args as { tone?: string }).tone ?? 'plain')}> <Story /> </main> ); },];
export const parameters = { layout: 'centered' };Rehearse reads the preview’s named exports, or its default export: decorators, args, argTypes, parameters,
loaders, render and initialGlobals (or globals). It combines them with the meta’s and the story’s as Storybook
does:
- args, argTypes and parameters: the preview’s, then the meta’s, then the story’s;
- decorators: the story’s innermost, then the meta’s, then the preview’s outermost;
- loaders: the preview’s, the meta’s, then the story’s, before the story renders;
- render: the story’s, else the meta’s, else the preview’s, else the meta’s
componentwith the args.
Other settings, all relative to the piece’s package:
| Setting | Default |
|---|---|
styles |
a stylesheet the package exports as ./styles.css (or ./style.css, ./styles, ./style, or any .css export), else src/styles.css, else src/index.css |
config |
vite.config.*, else vitest.config.*, else none |
viewport |
{ width: 1024, height: 768 } |
Import Storybook’s runtime
Section titled “Import Storybook’s runtime”A story or a preview may import Storybook’s runtime. Rehearse renders without Storybook, so it answers those imports itself:
| Import | What Rehearse gives |
|---|---|
storybook/preview-api, @storybook/preview-api, @storybook/client-api, @storybook/addons, @storybook/manager-api |
useArgs (args you can update), useGlobals, useParameter, useStoryContext, React’s hooks, and quiet stand-ins for the channel, makeDecorator, definePreview, composeStory and setProjectAnnotations |
storybook/test, @storybook/test |
fn() that records its calls; expect, userEvent, within, screen and waitFor that do nothing |
storybook/actions, @storybook/addon-actions |
action() and actions() that do nothing |
Render in light and dark
Section titled “Render in light and dark”List the schemes a piece renders in under schemes.
scenarios: pieces: paths: ['src/**/*.stories.tsx'] schemes: [light, dark]Each scheme renders the piece in its own browser with prefers-color-scheme set. The default is [light]. Each is
observed and compared apart, so differences say which: light tree line 2, dark tree line 2.
Every piece renders on a fixed clock (15 January 2026, 12:00 UTC), with reduced motion. A story that prints today’s date prints the same date on both sides.
Choose what is compared
Section titled “Choose what is compared”By default Rehearse compares, in each scheme:
| What | Why |
|---|---|
tree |
the accessibility tree: what a person using a screen reader gets, and what most visible changes change |
errors |
errors the page logged or threw while rendering |
violations |
axe violations by rule: WCAG 2.0 and 2.1 at levels A and AA, and WCAG 2.2 AA |
A change in errors or violations is a difference, never a failure. A piece fails only when it throws or never renders.
Compare screenshots only when asked
Section titled “Compare screenshots only when asked”Add screenshot to compare to compare screenshots too.
scenarios: pieces: paths: ['src/**/*.stories.tsx'] compare: [tree, errors, violations, screenshot]Screenshots are always taken and kept for the stage, but compared only when compare names screenshot. Pixels
differ across machines, fonts and GPUs, so a screenshot difference often means nothing. When you do compare them, a
difference is a changed hash: look at both on the stage.
Fix a piece that doesn’t render
Section titled “Fix a piece that doesn’t render”No Vite or React in the package. The run says it can’t render the story.
✗ A · One (src/A.stories.tsx#One) broke at runner: No package with Vite and React holds src/A.stories.tsx, so Rehearse can't render it.The story’s package (or a folder above it) needs vite, react and react-dom installed. On the base, a fresh
worktree has no node_modules until prepare installs them: set prepare: pnpm install --frozen-lockfile (or your
package manager’s) in rehearse.yaml. A piece that can’t be rendered for this reason is listed as not run, never as
failing.
A piece that never mounts. The run says it never rendered, with the first error the page logged.
✗ Missing · Default (src/Missing.stories.tsx#Default) render in light expected it renders, got It never rendered: Failed to load resource: the server responded with a status of 404 (Not Found)The story’s module, or something it imports, didn’t load, so React never rendered. Rehearse waits 20 seconds and gives the first error the page logged. Check:
- an import that doesn’t resolve in this package (a path alias your Vite config sets, but
configpoints elsewhere); - a provider the component needs: add it as a decorator in the preview module;
- a stylesheet or a font the preview imports that isn’t there.
To look at the page yourself, open it in Rehearse’s browser and read its tree.
rehearse drive start --piece 'src/Missing.stories.tsx#Default'rehearse drive snapshotrehearse drive stopA piece with nothing to render. The run says what is missing.
expected it renders, got This piece has no render, and its default export names no component.Give the story a render, or the meta a component.
A loader that fails shows A loader failed: …, with its message.