|
| 1 | +# 🎨 Chromatic Setup Guide |
| 2 | + |
| 3 | +This guide explains how to set up and use Chromatic for visual regression testing in cmux. |
| 4 | + |
| 5 | +## What is Chromatic? |
| 6 | + |
| 7 | +Chromatic is a visual testing platform that: |
| 8 | +- Captures screenshots of your Storybook components |
| 9 | +- Detects visual changes between commits |
| 10 | +- Provides a UI review workflow for accepting/rejecting changes |
| 11 | +- Integrates with GitHub PRs to show visual diffs |
| 12 | + |
| 13 | +## Initial Setup |
| 14 | + |
| 15 | +### 1. Create a Chromatic Account |
| 16 | + |
| 17 | +1. Go to [chromatic.com](https://www.chromatic.com/) |
| 18 | +2. Sign in with your GitHub account |
| 19 | +3. Click "Add project" and select the `coder/cmux` repository |
| 20 | + |
| 21 | +### 2. Get Your Project Token |
| 22 | + |
| 23 | +After creating the project, you'll receive a project token that looks like: |
| 24 | +``` |
| 25 | +chpt_1234567890abcdef |
| 26 | +``` |
| 27 | + |
| 28 | +### 3. Configure GitHub Secrets |
| 29 | + |
| 30 | +Add the Chromatic project token to your GitHub repository: |
| 31 | + |
| 32 | +1. Go to repository **Settings** → **Secrets and variables** → **Actions** |
| 33 | +2. Click **New repository secret** |
| 34 | +3. Name: `CHROMATIC_PROJECT_TOKEN` |
| 35 | +4. Value: Your Chromatic project token from step 2 |
| 36 | +5. Click **Add secret** |
| 37 | + |
| 38 | +### 4. Update Configuration File |
| 39 | + |
| 40 | +Edit `chromatic.config.json` and replace the placeholder with your actual project ID: |
| 41 | + |
| 42 | +```json |
| 43 | +{ |
| 44 | + "projectId": "your-actual-project-id-here" |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +You can find your project ID in the Chromatic dashboard URL or in the setup instructions. |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +### Running Chromatic Locally |
| 53 | + |
| 54 | +Test your Storybook components with Chromatic before pushing: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Set your project token (one-time setup) |
| 58 | +export CHROMATIC_PROJECT_TOKEN=chpt_your_token_here |
| 59 | + |
| 60 | +# Run Chromatic |
| 61 | +make chromatic |
| 62 | +# or |
| 63 | +bun run chromatic |
| 64 | +``` |
| 65 | + |
| 66 | +### Automated PR Checks |
| 67 | + |
| 68 | +Chromatic automatically runs on every pull request via GitHub Actions. The workflow: |
| 69 | + |
| 70 | +1. Builds your Storybook |
| 71 | +2. Captures screenshots of all stories |
| 72 | +3. Compares them against the baseline (main branch) |
| 73 | +4. Posts a status check and comment on your PR with: |
| 74 | + - Number of visual changes detected |
| 75 | + - Link to review changes in Chromatic UI |
| 76 | + - Direct comparison images |
| 77 | + |
| 78 | +### Reviewing Changes |
| 79 | + |
| 80 | +When Chromatic detects visual changes: |
| 81 | + |
| 82 | +1. Click the **"View changes in Chromatic"** link in the PR |
| 83 | +2. Review each component change in the Chromatic UI |
| 84 | +3. Accept or reject changes: |
| 85 | + - ✅ **Accept**: Updates the baseline for future comparisons |
| 86 | + - ❌ **Reject**: Keeps the existing baseline (treat as a regression) |
| 87 | + |
| 88 | +### Workflow Integration |
| 89 | + |
| 90 | +The Chromatic check is **informational** (won't block PRs) thanks to `exitZeroOnChanges: true`. This allows you to: |
| 91 | +- Review visual changes at your own pace |
| 92 | +- Merge PRs even with pending visual reviews |
| 93 | +- Accept/reject changes after merging if needed |
| 94 | + |
| 95 | +To make it a **required check** (block PRs with unreviewed changes): |
| 96 | +1. Update `.github/workflows/chromatic.yml`: Remove `exitZeroOnChanges: true` |
| 97 | +2. Add Chromatic as a required status check in GitHub branch protection |
| 98 | + |
| 99 | +## Best Practices |
| 100 | + |
| 101 | +### Component Development |
| 102 | + |
| 103 | +1. **Create stories for visual states**: Add Storybook stories for each significant visual state |
| 104 | +2. **Keep stories focused**: Each story should demonstrate one specific state or variant |
| 105 | +3. **Use controls**: Make stories interactive with Storybook controls for easier testing |
| 106 | + |
| 107 | +### Managing Changes |
| 108 | + |
| 109 | +1. **Accept intentional changes**: When you update styling, accept the Chromatic changes |
| 110 | +2. **Investigate unexpected changes**: If Chromatic catches changes you didn't make, investigate before accepting |
| 111 | +3. **Batch reviews**: Review multiple small changes together rather than one at a time |
| 112 | + |
| 113 | +### Performance |
| 114 | + |
| 115 | +- Chromatic uses **smart diffing** - only changed stories are captured |
| 116 | +- The workflow uses `onlyChanged: true` to speed up builds |
| 117 | +- First run takes longer (captures all baselines) |
| 118 | +- Subsequent runs are much faster |
| 119 | + |
| 120 | +## Troubleshooting |
| 121 | + |
| 122 | +### "Project token not found" Error |
| 123 | + |
| 124 | +Make sure you've added `CHROMATIC_PROJECT_TOKEN` to GitHub Secrets and it matches your Chromatic project token exactly. |
| 125 | + |
| 126 | +### Workflow Fails to Build Storybook |
| 127 | + |
| 128 | +Check that: |
| 129 | +1. `make storybook-build` works locally |
| 130 | +2. All dependencies are installed |
| 131 | +3. No TypeScript errors in story files |
| 132 | + |
| 133 | +### Visual Differences in Fonts/Colors |
| 134 | + |
| 135 | +This can happen due to OS-level rendering differences. Chromatic uses consistent rendering, but local previews might look different. Trust the Chromatic UI for accurate comparisons. |
| 136 | + |
| 137 | +### Too Many Changes Detected |
| 138 | + |
| 139 | +If you've refactored significantly: |
| 140 | +1. Review and accept legitimate changes in batches |
| 141 | +2. Consider using Chromatic's "Accept all" feature for major style updates |
| 142 | +3. Use Chromatic's branch comparison to see changes against your feature branch |
| 143 | + |
| 144 | +## Configuration Reference |
| 145 | + |
| 146 | +### chromatic.config.json |
| 147 | + |
| 148 | +```json |
| 149 | +{ |
| 150 | + "projectId": "your-project-id", |
| 151 | + "buildScriptName": "storybook:build", // npm script to build Storybook |
| 152 | + "storybookBuildDir": "storybook-static", // Where Storybook builds to |
| 153 | + "exitZeroOnChanges": true, // Don't fail on visual changes |
| 154 | + "exitOnceUploaded": true, // Exit after upload completes |
| 155 | + "autoAcceptChanges": false // Don't auto-accept changes |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +### Workflow Options |
| 160 | + |
| 161 | +In `.github/workflows/chromatic.yml`: |
| 162 | + |
| 163 | +- `onlyChanged: true` - Only test changed stories (faster) |
| 164 | +- `exitZeroOnChanges: true` - Don't block PRs on visual changes |
| 165 | +- `buildScriptName` - The npm/make script that builds Storybook |
| 166 | + |
| 167 | +## Resources |
| 168 | + |
| 169 | +- [Chromatic Documentation](https://www.chromatic.com/docs/) |
| 170 | +- [Storybook Documentation](https://storybook.js.org/docs) |
| 171 | +- [Visual Testing Best Practices](https://www.chromatic.com/docs/test) |
| 172 | + |
0 commit comments