--- name: storybook description: > Scaffolds, audits, and tests Storybook stories for React and React Native — a visual-regression `Default` story, an interactive `Playground`, jsdom unit tests by default, and Storybook interaction tests only for browser-only behavior and design decisions, with an opt-in adversarial `--validate` pass. Triggers on "scaffold stories", "add storybook", "story for this component", "/storybook". argument-hint: "[component-path] [--platform web|native] [--no-unit] [--no-interactions] [--no-playground] [--validate] [--auth ]" license: MIT metadata: author: mthines version: '1.0.0' workflow_type: scaffolder tags: - storybook - visual-regression - interaction-testing - jsdom - vitest - playwright - react - react-native - expo - chromatic - auth-flow - keychain --- # Storybook Scaffold and test Storybook stories. Per component: a `Default` story and a `Playground` story (both in the visual regression file). Plus a jsdom unit test file for everything jsdom can prove. Plus, only when needed, an interaction test file under a `/Tests` namespace. jsdom is the default because it is cheaper. Storybook interaction tests are reserved for browser-only behavior and for design decisions worth replaying in the Interactions panel ([`rules/test-placement.md`](./rules/test-placement.md)). Works on React (web) and React Native / Expo (native). Auth — when the running Storybook is gated — is opt-in and per-pathname, with secrets stored in the OS keychain instead of the repo. > **This `SKILL.md` is a thin index.** > Concern-specific rules live in [`rules/*.md`](./rules) and load on > demand. > Reference material lives in [`references/*.md`](./references). > Literal scaffolding lives in [`templates/*.md`](./templates). > Do not preload every file — load only what the current phase asks for. --- ## When to use Reach for this skill when any of the following is true: - A component has no story file yet and needs visual regression coverage. - An existing `*.stories.tsx` has no test counterpart (jsdom or Storybook). - A `Playground` story is missing or out of sync with current props. - The Storybook target is gated by auth and the agent needs to log in. - A flaky interaction test needs to be iterated against the running Storybook URL. Do **not** reach for this skill when: - The task is to build the component itself. Scaffold stories **after** the component is implemented and named. - The repo has no Storybook installation at all. Halt and ask the user to install Storybook first — this skill does not bootstrap Storybook. - The component is a hook or non-visual primitive with no rendered surface. Defer to [`tdd`](../../quality/tdd/SKILL.md). --- ## Arguments and defaults Parse `$ARGUMENTS` in this order: | Argument | Default | Effect | | -------------------------- | -------------------------------------------------------------------------- | ----------------------------------------------------------------------- | | `` | Prompt the user | Source file of the component to scaffold stories for. | | `--platform web\|native` | Auto-detect (see [`rules/react-native.md`](./rules/react-native.md)) | Web emits `.stories.tsx`; native emits the Expo / RN variant. | | `--no-unit` | Generate `.test.tsx` when a jsdom runner exists | Skip the jsdom unit test file. | | `--no-interactions` | Generate `.test.stories.tsx` when a behavior needs the browser | Skip the Storybook interaction test file. | | `--no-playground` | Include a `Playground` story | Skip the Playground story. | | `--no-default` | Include a `Default` story with grouped variants | Skip the visual regression Default story (rare — only for test-only). | | `--validate` | Off | Run the opt-in adversarial validate-and-fix phase (Phase 6) after the scaffold. Local only. | | `--auth ` | None | Use the named auth profile when running Playwright against the URL. | | `--title `| Inferred from the component path | Override the Storybook `title` for the generated meta. | Sub-commands: ```text /storybook validate # Run only the adversarial validate-and-fix phase against an existing story. /storybook auth list # List configured auth profiles in this repo. /storybook auth add # Register a new profile (writes config + stores secret in OS keychain). /storybook auth remove # Remove a profile (deletes config entry + keychain item). /storybook auth test # Dry-run the login flow against the configured URL. ``` `/storybook validate ` skips scaffolding: it reads the existing `.stories.tsx` / `.test.stories.tsx` and runs Phase 6 directly. The full loop — probe catalog, triage, confidence-gated fixes — lives in [`rules/adversarial-validation.md`](./rules/adversarial-validation.md). The full auth contract — config schema, keychain commands per OS, and the `storageState` reuse loop — lives in [`rules/auth.md`](./rules/auth.md). --- ## Workflow (six phases + opt-in validation) Each phase has a single gate. Do not proceed until it passes. Phase 6 runs only when `--validate` is passed or the skill is invoked as `/storybook validate `. | Phase | Name | Gate | | ----- | -------------------------- | --------------------------------------------------------------------- | | 0 | Preflight | Storybook installed; platform and jsdom runner detected; auth profile loaded if asked | | 1 | Component inspection | Component file read; props, variants, and behaviors classified jsdom / storybook | | 2 | Visual regression scaffold | `.stories.tsx` written; variants grouped | | 3 | Playground scaffold | `Playground` story written with `args` + `argTypes` | | 4 | Test scaffold (jsdom first)| `.test.tsx` written; `.test.stories.tsx` only if a behavior needs the browser | | 5 | Verification | jsdom tests pass; Storybook running; Playwright CLI confirms rendering | | 6 | Adversarial validation | (opt-in) Probes pass or defects fixed and pinned by a regression test | ### Phase 0 — Preflight Run these read-only checks before writing anything: ```bash # 1. Storybook installed? jq '.devDependencies | keys[]' package.json | grep -E '@storybook/' # 2. Storybook config dir present? ls .storybook/ 2>/dev/null # 3. Platform — web (Vite / Next) or native (Expo / RN)? Check # dependencies, devDependencies, AND peerDependencies — shared # component libraries (`packages/ui`) consumed by an Expo app # declare `react-native` as a peer. jq '(.dependencies // {}) + (.devDependencies // {}) + (.peerDependencies // {}) | keys[]' package.json | grep -E '^(react-native|expo|@storybook/react-native)' # 4. jsdom runner — vitest / jest with a jsdom or happy-dom environment? # Full detection in rules/test-placement.md. Set JSDOM_RUNNER. ``` Decision table: | State | Action | | -------------------------------------------------------- | ----------------------------------------------------------------------------- | | Storybook installed + `.storybook/` present | Proceed to Phase 1. | | Storybook missing | **Halt.** Ask user to install Storybook before invoking this skill again. | | `react-native` or `expo` in dependencies | Set `--platform native`. Load [`rules/react-native.md`](./rules/react-native.md). | | Neither present | Set `--platform web`. | | `--auth ` passed | Load [`rules/auth.md`](./rules/auth.md). Resolve secret before Phase 5. | | jsdom runner found | `JSDOM_RUNNER=vitest\|jest`. Load [`rules/test-placement.md`](./rules/test-placement.md). | | No jsdom runner | `JSDOM_RUNNER=none`. Install nothing; route every behavior to Storybook; print one line recommending jsdom setup. | ### Phase 1 — Component inspection Read the component file. Enumerate, in this order: 1. The component's exported name. 2. Each prop, its TypeScript type, and whether it is optional. 3. Each visible state derivable from props (`isLoading`, `error`, `disabled`, `variant`, `size`). 4. Each callback prop that an interaction test will need to spy on (`onClick`, `onChange`, `onSubmit`, …). 5. Any context providers the component requires (theme, query client, router) — these go into the meta `decorators`. 6. Each behavior to test, classified with the placement table in [`rules/test-placement.md`](./rules/test-placement.md) as `jsdom` or `storybook`, each with a one-word reason (`logic`, `DOM state`, `browser-only`, `design-decision`). Output the inventory inline before scaffolding, ending with a `behavior | jsdom/storybook | reason` table. The user reads it and corrects misinterpretations before any file is written. ### Phase 2 — Visual regression scaffold Write `.stories.tsx` next to the component file. Full rules and the variant-grouping pattern live in [`rules/create-stories.md`](./rules/create-stories.md). The shape, in one diagram: ```text default export (Meta) └── title: "/" └── component: └── parameters: { layout: "padded" | "centered" | "fullscreen" } export const Default: Story = { render: () => ( ), }; ``` Use the literal scaffolding in: - [`templates/component.stories.tsx`](./templates/component.stories.tsx) (web) - [`templates/component.native.stories.tsx`](./templates/component.native.stories.tsx) (React Native / Expo) If the repo already exports a `Stories` / `Stories.Entry` helper (checked by `Grep "Stories\\.Entry"` against the repo), use it to group variants into a single snapshot. Otherwise emit a plain flex/stack wrapper. The decision tree is in [`rules/create-stories.md`](./rules/create-stories.md). ### Phase 3 — Playground scaffold Append a `Playground` story to the same file. Every prop becomes an `argTypes` control. Mapping: | TypeScript prop | `argTypes` control | | ---------------------------------------- | --------------------------------------------------------------- | | `boolean` | `{ control: "boolean" }` | | `string` | `{ control: "text" }` | | `number` with inferable min/max | `{ control: { type: "range", min: , max: , step: } }` | | `number` without explicit clamps | `{ control: "number" }` | | Union of string literals | `{ control: "select", options: [...] }` | | Callback (`onClick`, `onChange`) | `args: { onClick: fn() }` — imported from `storybook/test` | | `ReactNode` / children | `{ control: "text" }` with a default string | Full example: see "Playground story" in [`rules/create-stories.md`](./rules/create-stories.md). ### Phase 4 — Test scaffold (jsdom first) Place each behavior where the Phase 1 table put it. Full decision table and the design-decision story contract: [`rules/test-placement.md`](./rules/test-placement.md). **4a — jsdom file** (skip with `--no-unit` or when `JSDOM_RUNNER=none`). Write `.test.tsx` next to the component, using the repo's existing suffix if it differs (`.spec.tsx`). Build it from the sibling stories with `composeStories`. Use [`templates/component.test.tsx`](./templates/component.test.tsx). On native, logic tests use `@testing-library/react-native` under Jest ([`rules/react-native.md`](./rules/react-native.md)). **4b — Storybook file** (skip with `--no-interactions`). Write `.test.stories.tsx` **only if at least one behavior is classified `storybook`**. If every behavior is jsdom-provable, write no file and say so. Every story either names its browser-only reason or carries a `Decision: … Why: …` description. **Hard rules for `.test.stories.tsx`** (full reasoning in [`rules/interaction-tests.md`](./rules/interaction-tests.md)): - File suffix is `.test.stories.tsx`. Do not use `.interactions.stories.tsx`, `.spec.stories.tsx`, or anything else. - Title is `/Tests`. One Storybook namespace per file — CSF3 allows exactly one default export. - The meta carries the repo's runner tag (see the rule) so the test runner picks it up. - `parameters.chromatic = { disableSnapshot: true }` keeps these stories out of visual regression. - All `userEvent` calls are awaited; all `expect` calls are awaited. - Locator priority: `getByRole` → `getByLabelText` → `getByPlaceholderText` → `getByText` → `getByTestId`. Never start with `getByTestId`. Use the literal scaffolding in: - [`templates/component.test.tsx`](./templates/component.test.tsx) (jsdom unit test) - [`templates/component.test.stories.tsx`](./templates/component.test.stories.tsx) (web Storybook test) - [`templates/component.native.test.stories.tsx`](./templates/component.native.test.stories.tsx) (React Native / Expo — uses `@storybook/react-native` test helpers) ### Phase 5 — Verification Start Storybook in the background and iterate against the live URL. Full Playwright CLI loop in [`rules/playwright-cli.md`](./rules/playwright-cli.md). Minimum verification, in this order: 1. jsdom tests pass via the repo's command (`vitest run ` / `jest `), when `.test.tsx` was written. 2. Story compiles — Storybook prints no error in the terminal. 3. Story renders — Playwright CLI navigates to the story URL and the canvas root selector resolves. 4. Default story is reachable from the sidebar at the inferred title. 5. Interaction test passes via the Storybook test runner (e.g. `npm run test-storybook`) — if the runner is wired up. For visual evidence (a screenshot or short clip of the rendered story), delegate to the [`pr-reviewer`](../../../agents/pr-reviewer.md) agent or the [`screencast`](../../analysis/screencast/SKILL.md) skill. Full handoff rules in [`rules/visual-verification.md`](./rules/visual-verification.md). If the Storybook URL is gated and `--auth ` was passed, the Playwright CLI invocation reuses the `storageState.json` produced by the auth profile's login flow. The skill never types credentials into the Playwright CLI directly. ### Phase 6 — Adversarial validation (opt-in, `--validate`) Runs only when `--validate` is passed, or standalone via `/storybook validate `. It is **local and interactive** — it drives Playwright against the running Storybook, so it never runs in CI. Phase 5 proves the story renders. Phase 6 proves the component **works**, then tries to **break it**: hostile inputs, rapid and duplicate events, keyboard-only navigation, viewport extremes, and error states. Each defect is triaged as a story bug or a component bug, fixed behind a confidence gate, and pinned with a regression test. The loop, in one line: probe → triage → fix (gated) → pin with a regression test → re-validate. - **Same tool as Phase 5**, not a second browser path. Probes run as a short Playwright script under `.agent/storybook/.probe/` (gitignored) so they can capture console errors, page errors, and failed requests — the signals a bare screenshot misses. - **Story bugs** (bad args, missing decorator, wrong locator) are fixed in the story or test directly — no gate. - **Component bugs** (crash, unescaped input, handler firing while disabled, focus trap, overflow) are gated: score with [`confidence`](../../quality/confidence/SKILL.md), apply only at **≥ 90**, and report sub-90 findings for the user to decide. Every applied fix is pinned by a regression case — in `.test.tsx` when jsdom can prove the defect, otherwise in `.test.stories.tsx`. - **Never** swallow the error, delete a probe, or `.skip` a test to force a green run. Full probe catalog, the harness pattern, triage rules, and the loop cap live in [`rules/adversarial-validation.md`](./rules/adversarial-validation.md). --- ## Decision flow at a glance | Signal | Do | | --------------------------------------------------------- | ------------------------------------------------------------------------ | | Component has no `.stories.tsx` | Run the full workflow (Phases 0–5). | | Component has `.stories.tsx` but no test files | Run Phase 0 (preflight) and Phase 1 (classify behaviors), then Phase 4. Reuse the existing meta `title` for any `/Tests` peer. | | Behavior jsdom can prove (callback, disabled, validation) | Write it in `.test.tsx`. No Storybook story. | | Behavior needs layout, CSS, or real focus; or is a design decision | Write it in `.test.stories.tsx` with a browser-only reason or `Decision:/Why:`. | | Existing `.test.stories.tsx` holds logic-only stories | Propose moving them to jsdom. Never delete without asking. | | `--validate` passed, or `/storybook validate ` | Run Phase 6. Load [`rules/adversarial-validation.md`](./rules/adversarial-validation.md). Local only. | | Running in CI | Skip Phase 6 — it drives a live browser and is not reproducible. | | Repo has `react-native` or `expo` | Set `--platform native`. Load [`rules/react-native.md`](./rules/react-native.md). | | Storybook URL is `http://localhost:6006/...` (or similar) | Iterate via Playwright CLI. No auth needed. | | Storybook URL is gated (login form, SSO, basic auth) | Resolve `--auth ` first. Load [`rules/auth.md`](./rules/auth.md).| | Visual evidence requested in the PR | Delegate to [`pr-reviewer`](../../../agents/pr-reviewer.md) or `screencast`. | | Component is a hook or non-visual | Stop. Defer to [`tdd`](../../quality/tdd/SKILL.md). | | Storybook isn't installed | Halt. Ask the user to install Storybook before re-running. | --- ## Composes with - [`tdd`](../../quality/tdd/SKILL.md) — owns hook and non-visual tests; component jsdom tests are written here (Phase 4a). This skill defers below the visual surface to it. - [the `tdd` provenance check](../../quality/tdd/rules/test-provenance.md) — run on the generated `.test.tsx` and `.test.stories.tsx` to ensure the test imports production component code, not a private shim. - [`pr-reviewer`](../../../agents/pr-reviewer.md) — runs the visual check pass and posts screenshots on the PR (Critical / High motion findings, visual diffs). - [`screencast`](../../analysis/screencast/SKILL.md) — captures short videos of multi-frame interactions where a still screenshot cannot prove the change (transitions, focus order, hover-revealed UI). - [`ux`](../ux/SKILL.md) — call after stories are scaffolded to audit the rendered states for accessibility and microcopy. - [`visual-design`](../visual-design/SKILL.md) — call **before** scaffolding when the component is new and the visual direction is not yet committed (palette, type, signature details). Call **after** scaffolding in `review` mode if the rendered variants look generic or drift from the chosen direction. Pairs with `ux` — `ux` audits the floor, `visual-design` shapes the ceiling. - [`confidence`](../../quality/confidence/SKILL.md) — gate before declaring the scaffold done if the user disputes a generated variant. --- ## Rules - [`rules/create-stories.md`](./rules/create-stories.md) — visual regression `*.stories.tsx` pattern: grouped variants, Playground story, layout parameter, static mock data only. - [`rules/test-placement.md`](./rules/test-placement.md) — jsdom-first placement table, runner detection, and the design-decision story contract. - [`rules/interaction-tests.md`](./rules/interaction-tests.md) — `*.test.stories.tsx` hard requirements: `/Tests` namespace, awaited `userEvent`, locator ladder, `fn()` spies, `step()` grouping. - [`rules/react-native.md`](./rules/react-native.md) — Expo / React Native flavor: `@storybook/react-native` differences, native locators, mobile-only template paths. - [`rules/auth.md`](./rules/auth.md) — opt-in per-pathname auth: config schema, OS-keychain commands, `storageState` reuse, multi-profile routing, secret rotation. - [`rules/playwright-cli.md`](./rules/playwright-cli.md) — iteration loop against the running Storybook URL: snapshot mode, `--last-failed`, iframe routes, story permalinks, headed vs headless. - [`rules/adversarial-validation.md`](./rules/adversarial-validation.md) — opt-in Phase 6 (`--validate`): the probe harness, the adversarial catalog, story-vs-component triage, the confidence-gated fix path, and the loop cap. - [`rules/visual-verification.md`](./rules/visual-verification.md) — delegation rules for the `pr-reviewer` agent and `screencast` skill, when each is the right tool. - [`rules/anti-patterns.md`](./rules/anti-patterns.md) — full list of patterns to refuse. ## References - [`references/test-placement.md`](./references/test-placement.md) — why jsdom is the default and what only a browser can prove. - [`references/platform-detection.md`](./references/platform-detection.md) — how the skill decides web vs native, and the Storybook builder matrix per framework. - [`references/flake-cookbook.md`](./references/flake-cookbook.md) — copy-paste decorator snippets for the five most common flake sources (animations, fonts, dates, random IDs, theme drift). Load only when an existing story has flaked or the user asks for deterministic snapshots — adding all five preemptively is cargo-culting. ## Templates - [`templates/component.stories.tsx`](./templates/component.stories.tsx) — web visual regression scaffold. - [`templates/component.test.tsx`](./templates/component.test.tsx) — jsdom unit test scaffold (web; native uses `@testing-library/react-native`, see [`rules/react-native.md`](./rules/react-native.md)). - [`templates/component.test.stories.tsx`](./templates/component.test.stories.tsx) — web design-decision interaction test scaffold. - [`templates/component.native.stories.tsx`](./templates/component.native.stories.tsx) — React Native / Expo visual regression scaffold. - [`templates/component.native.test.stories.tsx`](./templates/component.native.test.stories.tsx) — React Native / Expo interaction test scaffold. - [`templates/auth.config.example.json`](./templates/auth.config.example.json) — auth-profile config schema with two example profiles. --- ## Anti-patterns (one-liner — full list in [`rules/anti-patterns.md`](./rules/anti-patterns.md)) - Mixing visual regression stories and interaction tests in the same file. - Naming interaction tests `*.interactions.stories.tsx` or `*.spec.stories.tsx` instead of `*.test.stories.tsx`. - Dynamic values (`new Date()`, `Math.random()`) in story args — guarantees Chromatic / Loki false positives. - Putting jsdom-provable logic (callback fires, disabled blocks) in a Storybook play test. - Asserting layout or CSS in a jsdom test. - A `.test.stories.tsx` story with no browser-only reason and no `Decision:/Why:` description. - Skipping the locator ladder and reaching straight for `getByTestId`. - Storing Storybook credentials in `.env`, `package.json`, or the story file itself. - Generating a `Playground` story whose `argTypes` do not match the component's actual prop types. - Running Playwright CLI in headed mode in CI. - Running `--validate` in CI — it drives a live browser and is not reproducible. - Under `--validate`, forcing a green run by swallowing the error, deleting a probe, or `.skip`-ing a test, or editing component source below a confidence of 90. --- ## Definition of done - [ ] Phase 0 preflight passed; platform set; auth profile resolved if `--auth` was given. - [ ] `.stories.tsx` written with a `Default` story (grouped variants) and a `Playground` story (unless `--no-playground`). - [ ] Every behavior classified jsdom or storybook, with a reason. - [ ] `.test.tsx` written for jsdom-provable behavior (unless `--no-unit` or no jsdom runner). - [ ] `.test.stories.tsx` written under the `/Tests` namespace only if a behavior needs the browser (unless `--no-interactions`); each story has a browser-only reason or `Decision:/Why:`. - [ ] jsdom tests pass via the repo's test command. - [ ] Storybook compiles with no terminal errors. - [ ] Playwright CLI confirms the story URL renders. - [ ] Interaction test passes via the Storybook test runner (if the runner is wired up in the repo). - [ ] No credentials in the generated files. - [ ] If `--auth ` was used, `storageState.json` is written under `.agent/storybook/.auth/` and listed in `.gitignore`. - [ ] If `--validate` was used: applicable probes ran; findings triaged as story or component; component fixes applied only at confidence ≥ 90 and pinned by a regression test (jsdom when provable); sub-90 findings reported; the ephemeral probe script deleted and Storybook killed on exit.