# Storybook

Here is what [Variance Authority](https://variance-authority.dev/docs/index.md) does for your Storybook: it
records which lines of your code the preview ran to show each story, and
`variance run` shows each story as a test. One addon and one collector option
write the record. Then CI shows only the stories your change reached, you can
read what one story ran, and visual review runs on your machine and in CI with
no hosted service. Stories that run under `@storybook/addon-vitest` are Vitest
tests, and [the Vitest page](https://variance-authority.dev/docs/vitest/index.md) covers them.

## Try it

```bash
npm install --save-dev @variance-authority/storybook-collector @variance-authority/cli
npx playwright install chromium
```

```js
// .storybook/main.js: add one entry to addons
addons: ['@variance-authority/storybook-collector'],
```

```js
// variance/storybook.mjs
import { storybookCollector } from '@variance-authority/storybook-collector';

export default storybookCollector({ source: { dirs: ['src'] }, tests: true });
```

Name that module and the built index in `variance.config.json`, with `"subjects":
{ "kind": "storybook", "index": "storybook-static/index.json", "collector":
"variance/storybook.mjs" }` beside the keys [write the run
config](https://variance-authority.dev/start/storybook/index.md#write-the-run-config) lists. Build the preview that
records, run once, change a source file, and run again:

```bash
npx storybook build --test -o storybook-static
variance run --config variance.config.json
variance run --config variance.config.json --since origin/main
```

```text
[not observed] story:checkout--summary
not affected by the diff against origin/main: none of its 4 components touched
```

Probes go in only under `--test` or a dev server, so the Storybook you publish
stays as it is.

## What you get

### Leave out the stories a change did not reach

`--since` leaves out each story whose approved image records no changed
component and whose recorded lines include none you changed. A preview built
before the edit is read against your checkout, and a file changed since the
build counts as not instrumented, so every story that ran it is observed.
[Running less of the suite](https://variance-authority.dev/docs/selecting/index.md) says how the two records join.

### Place shards by recorded time

`variance run --config variance.config.json --shard 2/3` places each story file
on one job by the time its stories took on the mainline's last run, and a last
job merges the reports. [Split stories and routes the same
way](https://variance-authority.dev/docs/sharding/index.md#split-stories-and-routes-the-same-way) prints the workflow.

### Read one story's test story

```bash
VARIANCE_AUTHORITY_STORY=1 variance run --config variance.config.json --subjects "story:cart--empty"
variance story --file src/cart.stories.tsx --name "Cart/Empty"
```

It lists the calls of your code the preview made to show that story, in order,
with the branch each one took. [Test stories](https://variance-authority.dev/docs/test-stories/index.md) shows how to read
one and compare two.

### Ask which stories cover a line

Every recorded run also writes one row per story per region, so `variance
covering --file src/cart.tsx --line 40` names the stories that ran line 40.
[Covering](https://variance-authority.dev/reference/packages/cli/index.md#covering-which-tests-covered-this-line)
lists every way to ask.

### Visual review

Every `variance run` paints each story, compares it with its approved image,
and reports a changed region with the component that drew it and its
`file:line`. Storybook keeps mounting, decorators and play functions.
[Compare Storybook stories against approved screenshots](https://variance-authority.dev/start/storybook/index.md)
sets it up, and works without the recording too.

## Not yet

- Recording from a build without `--test` or a dev server: the Storybook you
  publish gets no probes.
- Recording inside `@storybook/addon-vitest`: that run is a Vitest run, and
  [the Vitest seam](https://variance-authority.dev/reference/packages/sense/index.md#cut-a-vitest-run-down-to-a-diff)
  records it where you configured one.
- Skipping cases does not apply: a story is already the case.

## Go further

- [Skipping stories nothing touched](https://variance-authority.dev/reference/packages/storybook-collector/index.md#skipping-stories-nothing-touched): every collector option.
- [What a CI checkout needs](https://variance-authority.dev/docs/selecting/index.md#what-a-ci-checkout-needs) for `--since` in CI.
- [A result a cache returned](https://variance-authority.dev/docs/selecting/index.md#a-result-a-cache-returned): the gap one shared preview leaves.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts Storybook beside every other runner.
