Skip to content
Rehearsev1.2.0

Write and compare pieces

A piece is a story you already write, in a story file beside its component.

src/Badge.stories.tsx
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.

rehearse.yaml
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.

Terminal
$ rehearse compare --base main
4 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.

  1. Point the manifest at your stories. List the story files under scenarios.pieces.

    rehearse.yaml
    scenarios:
    pieces: ['packages/ui/src/**/*.stories.tsx']
  2. Write stories beside the component. Every named export is a story, except default and names starting with __. A meta’s includeStories and excludeStories are 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#Warning
    name the title’s last part and the story’s name (or storyName, or the export in words): Badge · A warning
  3. Check the package can render it. Rehearse renders a piece in the nearest folder above the story that has a package.json and can load vite, react and react-dom. It uses that package’s own Vite, with its vite.config.* (or vitest.config.*) when there is one, and its own React.

  4. Run it once by its id, with --json to see what it observed.

    Terminal
    rehearse run 'src/Badge.stories.tsx#Warning' --json

    The observation’s observed part 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 in artifacts, and shown on the stage.

  5. Compare. rehearse compare --base main renders each piece on the base and on the head and compares them.

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.

rehearse.yaml
scenarios:
pieces:
paths: ['src/**/*.stories.tsx']
preview: .storybook/preview.tsx

The following preview wraps every story in a main that carries its tone.

.storybook/preview.tsx
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 component with 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 }

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

List the schemes a piece renders in under schemes.

rehearse.yaml
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.

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.

Add screenshot to compare to compare screenshots too.

rehearse.yaml
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.

No Vite or React in the package. The run says it can’t render the story.

Terminal
✗ 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.

Terminal
✗ 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 config points 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.

Terminal
rehearse drive start --piece 'src/Missing.stories.tsx#Default'
rehearse drive snapshot
rehearse drive stop

A piece with nothing to render. The run says what is missing.

Terminal
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.