All jobs

Add Snapshot Tests to a Vitest Component Suite

Yes. An agent adds Vitest snapshot tests for the UI components that have stories but no tests, keeps each snapshot small, and runs them in the existing CI job.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

In a React app laid out like alan2207/bulletproof-react, apps/react-vite runs vitest 2.1.4 through the test block in vite.config.ts: globals, a jsdom environment, src/testing/setup-tests.ts as the setup file and **/e2e/** excluded. There are twelve test files, all behavioral, and no __snapshots__ directory anywhere. Under src/components/ui, dialog, drawer, form and notifications have tests; button, spinner, link, table, md-preview and dropdown have a *.stories.tsx each and no test at all. .github/workflows/react-vite-ci.yml runs yarn test after yarn build, then yarn lint and yarn check-types, and src/testing/test-utils.tsx exports rtlRender for a bare render and renderApp for one inside the providers.

The agent writes snapshots where the rendered output is the contract and small enough to read in a diff. For Button, it renders every variant and size that buttonVariants in button.tsx declares, plus isLoading and icon, and snapshots the element's className inline with toMatchInlineSnapshot, so a change to a Tailwind class shows up as a one-line diff next to the test rather than a full DOM dump. For Spinner, the same for each size and variant from the two maps in spinner.tsx, and a check that the sr-only text is present. For MDPreview, it renders a markdown fixture that includes a heading, a link and a <script> tag and snapshots the resulting innerHTML to a file, which pins two things at once: how marked renders the fixture and that DOMPurify strips the script. For Link, it renders inside a memory router from react-router and snapshots the anchor's attributes. For Table, two rows through TableElement, TableHeader, TableBody and TableRow go to a file snapshot under __snapshots__.

It leaves the Radix-based components to the behavioral tests they already have: Dialog, Drawer and Dropdown render generated ids into aria-controls and id attributes, so a DOM snapshot of them churns without meaning. Each new test file sits in the component's __tests__ directory, the same layout as dialog/__tests__/dialog.test.tsx, and uses rtlRender from the test utils. The agent runs yarn test once to write the snapshots, commits the .snap files beside the tests, then runs CI=true yarn vitest run to show the suite passes in the mode CI uses, where Vitest does not write snapshots and a missing or obsolete one fails the run. To show what a failure looks like, it changes one class in button.tsx, runs the suite, quotes the diff Vitest prints, and reverts.

Then yarn lint, since eslint-plugin-vitest and eslint-plugin-testing-library apply to the new files, and yarn check-types. The workflow file does not change: the snapshots run inside the yarn test step that is already there.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add snapshot tests to the Vitest suite in apps/react-vite of this React app. Read vite.config.ts, src/testing/setup-tests.ts, src/testing/test-utils.tsx, every existing test under src, every component and story under src/components/ui, and .github/workflows/react-vite-ci.yml first. Write tests for the ui components that have a story but no test: button (every variant and size, plus isLoading and icon, as inline snapshots of className), spinner (every size and variant), link (inside a memory router), table (two rows, file snapshot) and md-preview (a markdown fixture with a heading, a link and a script tag, snapshot of the sanitized HTML). Do not snapshot the Radix components; say why. Put each test in the component's __tests__ directory using rtlRender. Run yarn test to write the snapshots, commit the .snap files, run CI=true yarn vitest run, then change one class in button.tsx to show the diff and revert it. Run yarn lint and yarn check-types. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that adds five test files under src/components/ui/*/__tests__, two .snap files under __snapshots__ for the table and the markdown preview, and nothing else: no component, config or workflow changes. The description lists each snapshot and what it pins, quotes the yarn test summary, the CI-mode run and the diff from the deliberate class change, and quotes yarn lint and yarn check-types.

The run's record holds every test run, including the one with the broken class, so a reviewer can read the exact diff a future change will produce before deciding whether these snapshots are worth keeping.

Before merging, a reviewer reads each .snap file once as if it were source, which is what it is from now on, and decides whether the markdown fixture covers the syntax their content uses. Updating a snapshot later is vitest -u, and the pull request says so beside the files.

Run it on every pull request that touches a ui component

An automation in the Project with a github trigger on pull requests fires on pull_request.opened and pull_request.synchronize. The trigger has no path filter, so its prompt asks the agent to answer in one line when the diff touches nothing under src/components/ui, and otherwise to run the suite, and when a snapshot fails, to read the diff against the component change and answer whether the new output looks intended, with the diff quoted. The trigger adds the pull request's title and description after the prompt as event text, and the automation is read-only, so it answers and never pushes; the answer is on the session.

A snapshot that is meant to change is then updated by the author with vitest -u, and the run on the next push reads the new .snap against the diff again. Each run is its own session with its own record, listed under the automation.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

Snapshot tests are only as good as the judgment behind what gets snapshotted, and that judgment needs the component source, the stories and the existing tests read side by side. In a Rush sandbox the agent reads all of them, writes the snapshots, and shows what a failure looks like on purpose before the pull request opens, so the reader sees the diff they are signing up for.

The runs stay in the record and the pull request quotes them, so review is reading the .snap files and the diff rather than running anything. A team with several apps on the same component layout runs the job on each at once, one sandbox and one pull request each.

Frequently asked questions

Why snapshot className for Button instead of the whole element?

Because the class string is the contract: buttonVariants in button.tsx is a cva definition whose entire output is classes, and the element around it is a button with children. An inline snapshot of the class string is one line per variant, reads in a diff, and fails only when a class changes.

Why no snapshots for Dialog, Drawer and Dropdown?

They are built on Radix primitives that render generated ids into attributes, so a DOM snapshot differs between runs for reasons that have nothing to do with the component. They already have behavioral tests that open, close and read them, which is the right kind of test for them.

What happens when someone forgets to commit a snapshot?

The CI job fails. Vitest does not write snapshots when CI is set, and a missing or obsolete snapshot fails the run, which is why the agent runs CI=true yarn vitest run in the sandbox and quotes it. The fix is vitest -u locally and committing the .snap file.

Does the markdown preview snapshot test DOMPurify?

It pins what the component outputs for a fixture that includes a <script> tag, which DOMPurify.sanitize removes in md-preview.tsx. If a future change to that line let the script through, the snapshot diff would show it. It is a regression test for this component's behavior, not a test of the library.

More Vitest jobs

The same job on other stacks

Keep reading