You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 089354e
Browse filesBrowse the repository at this point in the historyBrowse files
**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
+
45
49
### Environmental stability
46
50
47
51
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
54
58
- Screen scaling, color profiles, and display settings
55
59
- ...and occasionally what feels like the phase of the moon <MoonPhase />
56
60
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.
58
62
59
63
### Not a replacement for behavior testing
60
64
@@ -474,7 +478,7 @@ Running the visual regression suite in a shared environment solves this problem.
474
478
475
479
1.**Self-hosted runners** (e.g., Docker images), complex to set up and maintain
476
480
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
478
482
479
483
Options 2 and 3 are the quickest to get running, so those are covered below.
480
484
@@ -742,6 +746,166 @@ env:
742
746
743
747
Then run your tests like normal. The service handles the browser infrastructure.
744
748
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'
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
# ⚠️ 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/).
0 commit comments