Skip to content

Commit 089354e

Browse files
kylegachAriPerkkio
andauthored
docs: add Chromatic to VRT guide (#11341)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent 597df56 commit 089354e

1 file changed

Lines changed: 166 additions & 2 deletions

File tree

‎docs/guide/browser/visual-regression-testing.md‎

Lines changed: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ test('button renders in default state', async () => {
4242

4343
## Getting started
4444

45+
::: tip
46+
**Visual regression tests are most reliable when run in a standardized and tightly controlled environment**. This is also why [Docker containers](https://playwright.dev/docs/docker), [CI-only visual testing workflows, or cloud services](#visual-testing-for-teams) are strongly recommended.
47+
:::
48+
4549
### Environmental stability
4650

4751
Visual regression tests are **sensitive to environmental differences** because rendering is not perfectly deterministic across environments and depends on multiple factors:
@@ -54,7 +58,7 @@ Visual regression tests are **sensitive to environmental differences** because r
5458
- Screen scaling, color profiles, and display settings
5559
- ...and occasionally what feels like the phase of the moon <MoonPhase />
5660

57-
In practice, even seemingly identical environments can occasionally produce subtle rendering differences. For this reason, **visual regression tests are most reliable when run in a standardized and tightly controlled environment**. This is also why [Docker containers](https://playwright.dev/docs/docker), [CI-only visual testing workflows, or cloud services](#visual-testing-for-teams) are strongly recommended.
61+
In practice, even seemingly identical environments can occasionally produce subtle rendering differences.
5862

5963
### Not a replacement for behavior testing
6064

@@ -474,7 +478,7 @@ Running the visual regression suite in a shared environment solves this problem.
474478

475479
1. **Self-hosted runners** (e.g., Docker images), complex to set up and maintain
476480
1. **Generate references in CI**, which requires some setup
477-
1. **Cloud services**, like [Azure App Testing](https://azure.microsoft.com/en-us/products/app-testing/), built to solve this exact problem, but usually restricted to specific providers and browsers
481+
1. **Cloud services**, like [Azure App Testing](https://azure.microsoft.com/en-us/products/app-testing/) or [Chromatic](https://www.chromatic.com/vitest), built to solve this exact problem, but usually restricted to specific providers and browsers
478482

479483
Options 2 and 3 are the quickest to get running, so those are covered below.
480484

@@ -742,6 +746,166 @@ env:
742746
743747
Then run your tests like normal. The service handles the browser infrastructure.
744748
749+
=== Chromatic (Cloud service)
750+
751+
Chromatic offers a plugin to integrate with Vitest for visual regression testing. First, you run your tests as normal and Chromatic captures full DOM archives of your components' visual states. Then you run the Chromatic CLI to upload those archives, compute the visual diffs, and present the results for review in the Chromatic web app.
752+
753+
Because the DOM archives render in a consistent cloud browser environment, visual diffs are more reliable and less prone to false positives caused by local environment variations.
754+
755+
By default, the Chromatic plugin will automatically capture the end state of each test. You can also capture intermediate states by using the `takeSnapshot` function provided by the Chromatic plugin.
756+
757+
### Requirements
758+
759+
- Vitest version 4.0.0 and above
760+
- Vitest project must use `@vitest/browser-playwright`
761+
762+
### Get started
763+
764+
First, install the Chromatic plugin:
765+
766+
::: code-group
767+
```bash [npm]
768+
npm install -D @chromatic-com/vitest
769+
```
770+
```bash [yarn]
771+
yarn add -D @chromatic-com/vitest
772+
```
773+
```bash [pnpm]
774+
pnpm add -D @chromatic-com/vitest
775+
```
776+
:::
777+
778+
Then, add the plugin to your Vitest configuration:
779+
780+
```ts{3,6-8} [vitest.config.ts]
781+
import { defineConfig } from 'vitest/config'
782+
import { playwright } from '@vitest/browser-playwright'
783+
import { chromaticPlugin } from '@chromatic-com/vitest/plugin'
784+
785+
export default defineConfig({
786+
plugins: [chromaticPlugin({
787+
// ...options here: https://www.chromatic.com/docs/vitest/configure/
788+
})],
789+
test: {
790+
browser: {
791+
provider: playwright(),
792+
enabled: true,
793+
instances: [{ browser: 'chromium' }],
794+
},
795+
},
796+
})
797+
```
798+
799+
That's it! Your Vitest setup is now integrated with the Chromatic plugin for visual regression testing.
800+
801+
### Configuring tests
802+
803+
You can optionally configure test suites or individual tests. For example, to take additional snapshots at specific points in a test, you can use the `takeSnapshot` function as shown below.
804+
805+
```tsx{4,8-13,24-25,31-34} [accordion.test.tsx]
806+
import { expect, test } from 'vitest'
807+
import { page } from 'vitest/browser'
808+
import { render } from 'vitest-browser-react'
809+
import { configure, takeSnapshot } from '@chromatic-com/vitest'
810+
import { Accordion } from '../src/components/Accordion'
811+
812+
test('Can open accordion', async () => {
813+
// 👇 Configure Chromatic plugin for this test
814+
configure({
815+
// 👇 Prevent automatic snapshot at the end of the test
816+
disableAutoSnapshot: true,
817+
// ...more options here: https://www.chromatic.com/docs/vitest/configure/
818+
})
819+
820+
await render(<Accordion header="Example header">Example content</Accordion>)
821+
822+
const toggle = page.getByRole('button', { name: 'Example header' })
823+
const content = page.getByText('Example content')
824+
825+
// Open accordion, content should become visible
826+
await toggle.click()
827+
await expect.element(content).toBeInTheDocument()
828+
829+
// 👇 Call takeSnapshot to capture the component at this point in the test.
830+
await takeSnapshot()
831+
832+
// Close accordion, content should become hidden
833+
await toggle.click()
834+
await expect.element(content).not.toBeInTheDocument()
835+
836+
// You can call takeSnapshot multiple times if necessary.
837+
// To help disambiguate, you can give the snapshot a name,
838+
// which is passed as the first argument to takeSnapshot.
839+
await takeSnapshot('closed')
840+
})
841+
```
842+
843+
### Running tests
844+
845+
First, [create a Chromatic Vitest project](https://www.chromatic.com/signup) and take note of the token, which will be used in the final step below.
846+
847+
Second, run your Vitest tests as normal:
848+
849+
```bash
850+
npm run test # Your test script that runs vitest
851+
```
852+
853+
Finally, run the Chromatic CLI to upload your snapshots:
854+
855+
```bash
856+
npx chromatic --vitest -t=<YOUR_PROJECT_TOKEN>
857+
```
858+
859+
Once your visual regression tests finish, it will log a link where you can review the results.
860+
861+
### CI setup
862+
863+
Running in CI is exactly the same as running locally: first run `vitest`, then run `chromatic`.
864+
865+
To make this even more straightforward, Chromatic provides a GitHub Action:
866+
867+
```yaml [.github/workflows/chromatic.yml]
868+
name: Chromatic
869+
870+
on: push
871+
872+
jobs:
873+
tests:
874+
name: Run Vitest & Chromatic
875+
runs-on: ubuntu-latest
876+
steps:
877+
- name: Checkout code
878+
uses: actions/checkout@v7
879+
880+
- uses: actions/setup-node@v7
881+
with:
882+
node-version: 24
883+
884+
# ⚠️ See your package manager's documentation for the correct command to install dependencies in a CI environment.
885+
- name: Install dependencies
886+
run: npm ci
887+
888+
- run: npx playwright install chromium --only-shell
889+
890+
- name: Run Vitest tests
891+
run: npm run test
892+
893+
- name: Run Chromatic
894+
uses: chromaui/action@latest
895+
with:
896+
# ⚠️ Enable Vitest
897+
vitest: true
898+
# ⚠️ Make sure to configure a `CHROMATIC_PROJECT_TOKEN` repository secret
899+
projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
900+
# ⚠️ Optionally configure the archive location with env vars to match your outputDirectory https://www.chromatic.com/docs/vitest/configure/#test-run-options
901+
env:
902+
CHROMATIC_ARCHIVE_LOCATION: .vitest/chromatic
903+
```
904+
905+
If you're not using GitHub Actions, Chromatic has [guides for popular CI providers like GitLab, Bitbucket, and CircleCI](https://www.chromatic.com/docs/ci/).
906+
907+
Once you have Chromatic set up in your CI workflow, you'll now be able to [quickly review and collaborate on visual changes directly in the Chromatic app](https://www.chromatic.com/docs/in-pull-request/).
908+
745909
::::
746910
747911
### Picking the right option

0 commit comments

Comments
 (0)