sanity-visual-regression
GitHub规范 Sanity 仓库的视觉回归测试流程,定义 Storybook、Vitest 和 Playwright 三种快照源的职责边界与使用场景,确保 UI 变更得到全面覆盖。
Trigger Scenarios
Install
npx skills add sanity-io/sanity --skill sanity-visual-regression -g -y
SKILL.md
Frontmatter
{
"name": "sanity-visual-regression",
"description": "Add, review, and maintain Chromatic visual regression coverage in the Sanity monorepo via dev\/storybook stories, the vitest browser-mode suite, and Playwright e2e snapshots. Use when adding visual test coverage, writing stories, reviewing Chromatic diffs, working on the styled-components to vanilla-extract or @sanity\/ui to ui5 migrations, or when a PR needs visual regression protection."
}
Sanity Visual Regression Testing
Visual regression runs on Chromatic, wired via .github/workflows/chromatic.yml. Three snapshot sources, one Chromatic project each:
| Source | Chromatic project | Repo secret | Status |
|---|---|---|---|
dev/storybook stories |
"sanity studio" | CHROMATIC_PROJECT_TOKEN_STORYBOOK |
Active |
| Vitest browser tests (in place) | "sanity studio vitest" | CHROMATIC_PROJECT_TOKEN_VITEST |
Active |
Playwright e2e takeSnapshot() |
"sanity studio playwright" | CHROMATIC_PROJECT_TOKEN_E2E |
Active, curated opt-in |
All checks are non-gating during burn-in (exitZeroOnChanges); merges to main auto-accept
baselines. Review diffs on the Chromatic build linked from the PR check.
Which source owns a state
Each source owns a disjoint set of states. Decide by how the state is reached, and never move a state between sources:
| The state is… | It belongs in |
|---|---|
Reachable from props/fixtures alone, or one play interaction (open a menu, hover a tooltip) |
A *.stories.tsx in the owning package (dev/storybook snapshots it) |
| Reached by driving the UI: typing, drag, clipboard, focus tracking, viewport changes, server commands | A *.browser.test.tsx — its end state is the snapshot; takeSnapshot() for mid-test states (see below) |
| Full-studio chrome against a real deployment and dataset | A Playwright spec using e2e/studio-visual-test.ts ("sanity studio playwright" project), read-only states only |
Consequences:
- A
*.browser.test.tsxis the snapshot source for everything it renders — the Vitest plugin archives every test's end state with no test code changes. Browser tests keep their harness component inline (function FooHarness()in the test file) and never get a Storybook story on top. Do not re-export a browser test as a story, and do not port a browser test into a story with an elaborateplayfunction. - Every
*Story.tsxfile is a Storybook harness imported by a*.stories.tsx. Stories cover the states no browser test renders. - Playwright stays in
e2e/. Do not put specs,@chromatic-com/playwright, or e2e fixtures underdev/storybook, and do not turn an e2e spec into a story (or the reverse) to get a snapshot.dev/storybook'splaywrightdependency is only the browser runner for@storybook/addon-vitest. - Do not add a
Chromatic / …job that uploads one source's output to another source's project; Chromatic requires one project per integration type.
Quick start: add visual coverage for a component
Before writing anything, run pnpm visual-coverage <path> --prs and follow
sanity-visual-coverage. It reports whether a story already
renders the component or an open PR is about to, so you only add what is missing.
- Add a co-located
*.stories.tsxfile to the owning package'ssrctree, usually in the same__tests__directory as the component or harness. Storybook discovers story files in workspace packagesrctrees — do not add CSF underdev/storybook/stories/. Two patterns:- Plain component states (ui-components wrappers, tone/card sentinels): render variants directly — see Button.stories.tsx. Put many variants in one story (a grid) to keep snapshot count low.
- Studio-context states (form inputs, anything needing workspace/i18n/layers): wrap in the
shared mock-studio wrapper
TestWrapper(+TestFormfor form inputs) frompackages/sanity/test/browser— the same wrapper the browser tests use — see Dialog.stories.tsx and EditorChrome.stories.tsx. New coverage can use a colocated*Story.tsxwhen the grid needsTestWrapperinside the harness — see ConfirmPopover.stories.tsx. Do not extract a browser test's inline harness into a*Story.tsxto reuse it from a story: that state is snapshotted by the Vitest integration, and the story would be a duplicate snapshot of the same pixels. If the test's harness lacks a state you need, add it to the test, or build a separate story-only harness.
- Overlays that only exist after interaction (tooltips, menus, submenu flyouts) get a
playfunction usingstorybook/test(userEvent+waitFor/expect(...).toBeVisible(), queryingwithin(document.body)for portaled content). Chromatic and addon-vitest both runplaybefore capturing, so the snapshot shows the open overlay — see Tooltip.stories.tsx and MenuGroup.stories.tsx (the latter also documents an animation pitfall with nested popovers). Statically controllable overlays (e.g. Popover'sopenprop) don't needplay. - Verify locally:
pnpm dev:storybook(port 6006), thenpnpm --filter sanity-storybook test(every story runs as a vitest browser-mode test via@storybook/addon-vitest). - Push — the
Chromatic / Storybook visual testscheck snapshots only affected stories (TurboSnap) and links the build for review.
Migration priority: card and tone-related components first (tones cascade through everything),
box primitives later. Snapshot the wrapper components in packages/sanity/src/ui-components
and vanilla-extract-migrated components (change indicators, DocumentLayout) as sentinels.
Every story is browsable
The Storybook is a living document of how reusable components look and behave, and every story
in it is meant to be read: authored variant grids, component states and migration sentinels,
each with a concise JSDoc description of what it shows and why. Do not use story tags to hide
stories from the sidebar (!dev) or docs (!autodocs). That convention existed only for the
stories that re-exported vitest browser tests, and those are gone — browser tests are snapshotted
in place by the Vitest project. If a state is not worth a person looking at, it does not belong
in Storybook; drive it in a browser test instead (see "Which source owns a state").
A story that should be browsable but never snapshotted sets
parameters: {chromatic: {disableSnapshot: true}}.
Determinism rules for stories
- Harness stories are deterministic by construction (mock client/workspace, no network).
- Never render live timestamps, random ids, or unfinished loading states. Chromatic pauses CSS animations automatically.
- Knobs via
parameters.chromaticper story/meta:delay(ms before capture — Portable Text stories use 300 for editor boot),diffThreshold,disableSnapshot: true(story stays in Storybook + addon-vitest but is never snapshotted),modes(viewport/theme matrix — the global 1280×900 desktop mode in preview.tsx matches the vitest browser viewport).
Browser tests: automatic snapshots, opt-outs, targeted snapshots
Chromatic for Vitest Browser Mode
is generally available (@chromatic-com/vitest 1.x) and the "sanity studio vitest" project is
live: the Chromatic / Vitest browser visual tests job re-runs the suite on chromium with
CHROMATIC=1 on every PR and pushes the archives. Every *.browser.test.tsx is in the visual
suite without any code in the test, and the MyComponent.browser.test.tsx +
MyComponentStory.tsx + MyComponent.stories.tsx triple that existed only to snapshot a browser
test is gone (see "Which source owns a state").
- Automatic snapshot at the end of every test. Name it well: in Chromatic the snapshot is
describe chain / it title / Snapshot #nunder the test file's path. - Opt out with
configure({disableAutoSnapshot: true})from@chromatic-com/vitest. The scope follows where it is called: at the top level of a test file it applies to every test in the file; inside adescribe()to that suite and its nested suites; inside atest()to that test only. Use it for tests whose end state is not worth a snapshot (pure interaction checks, cleanup-only states) — the snapshot budget is per test case. - Targeted snapshots with
await takeSnapshot('state name')inside atest(), for states the test moves through but does not end on (a menu open before the click that closes it, a drag mid-way). Alwaysawaitit; the plugin fails the test on un-awaited calls. Docs: targeted snapshots. - Make the archived state explicit. Chromatic records which elements match
:hover/:focus/:activeat capture and re-applies them, so the real pointer position and React hover/focus state are part of the snapshot. End interactive tests withsettleChromaticEndState()(packages/sanity/test/browser/testHelpers.ts: parks the pointer, asserts a hover-free tree and no open tooltip, waits for toolbar/popover geometry) and assert the exact state you want captured (toBeEnabled(),data-focused="true", a visible menu) right before the test ends or beforetakeSnapshot(). Never fix a flaky snapshot with global CSS from the test setup (zeroed transitions, hidden carets, forced opacity). Reduced motion is a capture-time setting only: Chromatic renders the uploaded archive withprefersReducedMotion: 'reduce'andpauseAnimationAtEnd(vitest.browser.config.mts), which the@sanity/uiv5 stylesheet (ui5/styles.css) honors. The browser the tests run in — local runs, the functionalbrowser-tests.ymlshards and the capture run alike — has no reduced-motion emulation, so transitions run at full length there; wait for the settled state instead of assuming the media query shortened it. - Safe in every run. The plugin is registered in
vitest.browser.config.mtson every run, so both@chromatic-com/vitesthelpers (configure,takeSnapshot) work in plainpnpm --filter sanity test:browserruns and in the functionalbrowser-tests.ymlshards; those two are no-ops on firefox and webkit.settleChromaticEndState()is not part of the plugin and is not a no-op anywhere: it parks the pointer and runs its stability waits on every browser, so a test's end state is asserted in the functional shards too — do not skip it on firefox or webkit. OnlyCHROMATIC=1turns on capturing (automatic snapshots, TurboSnap stats, reporter output, Chromatic telemetry); a normal run writes nothing except the archive of an explicittakeSnapshot()call, into the gitignored.vitest/chromatic.
See REFERENCE.md for local capture runs, TurboSnap, sharding and cost controls.
Playwright e2e snapshots
e2e/studio-test.ts wraps @chromatic-com/playwright with auto-snapshots disabled globally —
the suite runs against per-PR staging datasets (live timestamps, presence, parallel mutations),
so blanket end-of-test snapshots would be pure diff noise. Opt in per spec with takeSnapshot()
at deterministic moments only. See REFERENCE.md before
adding e2e snapshots.
More
REFERENCE.md: architecture and rationale, local Chromatic runs, CLI flags,
snapshot cost management, Vercel deployment (studio-storybook.sanity.dev), troubleshooting.
Version History
-
584242a
Current 2026-09-23 01:00
修复 Vitest 快照稳定性;移除浏览器测试故事的重新导出并内联共享组件;新增视觉覆盖率检查器。
- a94b3d8 2026-08-28 22:14
- 3235fb3 2026-08-20 10:56


