Upload from CI
Storybook
Screenshot every Storybook story with @circi/storybook, cropped to the component, and upload them to CirCI so your team reviews each visual change.
@circi/storybook screenshots every story of a built Storybook with Playwright and uploads the PNGs to your CirCI server, which compares each one with its baseline and asks your team to review what changed. Each screenshot holds the component and nothing around it: no empty canvas, no Storybook interface.
Set it up
Section titled “Set it up”-
Install the addon and Playwright, and Playwright's browser:
Terminal window npm install --save-dev @circi/storybook playwrightnpx playwright install chromiumPlaywright is a peer dependency, so you choose its version. Node 22 or later is needed (22.18 or later to read a
.storybook/main.ts). -
List the addon in
.storybook/main.ts. Its options live there:.storybook/main.ts import { circi } from '@circi/storybook'export default {// your framework, stories and other addonsaddons: [circi({ name: 'storybook', viewports: ['1280x720'] })],} -
In CI, with the project's token as
CIRCI_TOKENand your CirCI address asCIRCI_API_URL(Create a project), build Storybook and upload:Terminal window npx storybook buildnpx circi-storybook upload --wait--buildrunsstorybook buildfor you instead. Without the token or the address, the screenshots are written to.circi/screenshots/storybookand nothing is uploaded, so you can look at them locally. -
Open the build in the dashboard. The first build has nothing to compare with, so approve every screenshot once (First review).
A story's screenshot is named by its title and story name: Components/Button and Primary become components/button/primary. Renaming a story starts a new baseline.
What a screenshot is
Section titled “What a screenshot is”- Most stories: cropped to what the story renders, with its box shadows, plus anything it renders into the page outside Storybook's root (tooltips, menus and dialogs in portals). The
centeredandpaddedlayouts add no whitespace. layout: 'fullscreen': the whole page, at the viewport's width and the content's full height.- A story that renders nothing fails with "rendered nothing" instead of uploading a blank image.
Before each screenshot the story has to be still: Storybook has reported it rendered and its play function finished, fonts and images are loaded, and animations, transitions and the caret are off. A story that throws, or whose play function throws, is reported with its message. The other stories still run, but nothing is uploaded and the command exits 2: a partial build would show the failed stories as removed.
Per story
Section titled “Per story”parameters.circi (typed as CirciParameters) changes one story:
import type { CirciParameters } from '@circi/storybook'
export const Open = { parameters: { circi: { viewports: ['390x844', 'wide=1440x900'], delay: 200 } satisfies CirciParameters, },}| Key | What it does |
|---|---|
skip: true |
Leaves the story out. |
viewports |
Replaces the configured viewports for this story. |
selector |
Captures that element instead of the story's content. |
padding |
Pixels of space around the crop. |
delay |
Milliseconds to wait after the story renders. |
A viewport the story chose with Storybook's Viewport addon (globals.viewport, or parameters.viewport.defaultViewport in Storybook 8) is its screenshot's viewport, unless parameters.circi.viewports is set. With more than one viewport or browser, a screenshot's name gets @<viewport> or @<browser>: components/button/primary@mobile.
Options
Section titled “Options”All optional. In circi({ … }) they are circi.Options; circi-storybook upload takes each as a flag, which overrides the config.
| Option | Flag | What it does |
|---|---|---|
name |
--name |
Build name. Default storybook. |
wait |
--wait |
Wait for the result; exit 1 while changes need review and this job is the gate. |
strict |
--strict |
Exit 2 if the upload fails. |
shard |
--shard 2/4 |
Screenshot and upload one share of the stories, as one shard of a parallel build. |
browsers |
--browser |
chromium (default), firefox, webkit. |
viewports |
--viewport |
1280x720 (default), or named: mobile=390x844. |
include, exclude |
--include, --exclude |
Globs over a story ID or Title/Story name. |
concurrency |
--concurrency |
Stories screenshotted at once. Default 4. |
site |
--site [name] |
Also upload the built Storybook as a hosted site, so reviewers can open the live story. |
staticDir, build |
the folder argument, --build |
The built Storybook (default storybook-static), and whether to build it first. |
dir, clean, root |
--dir, --clean, --root |
Where screenshots are written (default .circi/screenshots, in its storybook folder), whether to empty that folder first, and the project root. |
apiUrl, token |
--api-url, CIRCI_TOKEN |
Default CIRCI_API_URL and CIRCI_TOKEN. Keep the token in the environment. |
apiUrl, token, name, dir, clean, root, wait, strict and shard mean the same as in the Vitest plugin. Exit codes: 0 done, 1 changes need review (with --wait), 2 a story failed, bad flags or setup, or a failed upload with --strict.
Give the build its own name if the same commit also uploads other screenshots (several builds per commit). The complete CI workflow, with the pull request check, is in the GitHub Actions guide: replace its test and upload steps with the two commands above.
Without the addon: Vitest and portable stories
Section titled “Without the addon: Vitest and portable stories”If your components already have Vitest browser tests, a test can render each story with Storybook's portable stories and let the Vitest plugin capture it. This covers the story files you list in a test, and Storybook's .storybook/preview setup applies only if you pass it to setProjectAnnotations (see Storybook's portable stories documentation).
import { composeStories } from '@storybook/react-vite'import { expect, test } from 'vitest'import { render } from 'vitest-browser-react'import * as stories from './Button.stories.jsx'
// One screenshot per story, named by the story's export: "Primary" is the name "primary".for (const [name, Story] of Object.entries(composeStories(stories))) { test(name, async () => { const screen = await render(<Story />) await expect.element(screen.container).toMatchScreenshot(name.toLowerCase()) })}With circi({ name: 'storybook' }) in the Vitest config, CIRCI_API_URL=… CIRCI_TOKEN=… npx vitest run uploads them as src/Button.visual.test.jsx/primary and so on (names). This route was run end to end with Storybook 10.6.8, Vitest 5.1.1 and @circi/vitest 0.3.0 against a real CirCI server.
Make story screenshots stable
Section titled “Make story screenshots stable”CirCI marks a pixel as changed once its color moves past a fixed tolerance, and the default diff threshold is 0% (How screenshots are compared). Take the screenshots on the same machine image every time (fonts differ between operating systems), and keep data and dates in stories fixed. For a part that still moves, draw an ignore region.