This guide explains how to set up and run visual regression tests for the Brain-Storm frontend.
Visual regression testing automatically detects unintended UI changes by comparing screenshots across versions. We use two complementary tools:
- Chromatic - Component-level visual testing via Storybook
- Percy - Full application visual testing via Playwright
Chromatic integrates with Storybook to test individual components in isolation.
Features:
- Component snapshot testing
- Automatic baseline creation
- Visual diff review in PRs
- Accessibility checks
Setup:
# Already configured in .github/workflows/ci.yml
# Requires CHROMATIC_PROJECT_TOKEN secretRunning Locally:
npm run build-storybook --workspace=apps/frontend
npx chromatic --project-token=<YOUR_TOKEN>Percy captures full-page screenshots of the running application for comprehensive visual testing.
Features:
- Full-page visual regression detection
- Responsive design testing (mobile, tablet, desktop)
- Dark mode testing
- Component interaction testing
- Visual diff review in PRs
Setup:
# Install Percy CLI
npm install -g @percy/cli
# Set token
export PERCY_TOKEN=<YOUR_TOKEN># Build Storybook
npm run build-storybook --workspace=apps/frontend
# Run Chromatic
npx chromatic --project-token=<YOUR_TOKEN># Start backend
npm run dev:backend
# In another terminal, run visual tests
cd apps/frontend
npx playwright test --config=playwright-visual.config.tsVisual tests run automatically on:
- Pull requests
- Pushes to main branch
Chromatic:
- Runs on every PR and main push
- Creates visual diffs for review
- Blocks merge if critical changes detected
Percy:
- Runs on every PR and main push
- Captures full-page screenshots
- Compares against baseline
- Uploads results to Percy dashboard
-
Dashboard
- Full page layout
- Responsive layouts (mobile, tablet, desktop)
- Dark mode variant
- Header and navigation
-
Courses
- Course listing page
- Course detail page
- Enrollment modal
- Dark mode variant
-
User Profile
- Profile page layout
- Achievements section
- Credentials section
- Dark mode variant
-
Authentication
- Login page
- Signup page
- Error states
- Form validation
-
Components
- Button states
- Form inputs
- Modals and dialogs
- Notifications and alerts
Tests cover three viewport sizes:
- Mobile: 375x667 (iPhone SE)
- Tablet: 768x1024 (iPad)
- Desktop: 1920x1080 (Full HD)
Tests include:
- Light mode (default)
- Dark mode (emulated)
-
Chromatic Review
- Check the Chromatic status check
- Click "Review changes" to see visual diffs
- Approve or request changes
-
Percy Review
- Check the Percy status check
- Click "Review changes" to see full-page diffs
- Approve or request changes
- Green: No changes detected
- Yellow: Minor changes (review recommended)
- Red: Significant changes (review required)
Baselines are automatically updated when you accept changes in the Chromatic UI.
Baselines are automatically updated on main branch after approval.
To manually update:
# Run tests and approve changes
npx playwright test --config=playwright-visual.config.ts --update-snapshotsSome elements change on every run (timestamps, IDs, etc.). Mask them to avoid false positives:
await expect(page).toHaveScreenshot('page.png', {
mask: [page.locator('[data-testid="dynamic-content"]')],
});Cause: Different fonts, rendering engines, or OS differences
Solution:
- Use Docker for consistent environment
- Run in CI environment
- Check browser versions
Cause: Dynamic content, animations, or timing issues
Solution:
- Add masks for dynamic elements
- Wait for animations to complete
- Use
waitForLoadState('networkidle')
Cause: Intentional design changes not approved
Solution:
- Review and approve changes in Percy/Chromatic
- Update baselines after approval
- Document design changes in PR
-
Keep Tests Focused
- Test one component or page per test
- Avoid testing multiple states in one screenshot
-
Wait for Content
- Use
waitForLoadState('networkidle') - Wait for animations to complete
- Wait for images to load
- Use
-
Mask Dynamic Content
- Mask timestamps, IDs, random content
- Mask user-generated content
- Mask external API responses
-
Review Changes Carefully
- Always review visual diffs
- Understand why changes occurred
- Approve intentional changes only
-
Keep Baselines Updated
- Update baselines after design changes
- Document baseline updates in commits
- Avoid accumulating drift
CHROMATIC_PROJECT_TOKEN- Chromatic project tokenPERCY_TOKEN- Percy project token
.github/workflows/ci.yml- Main CI workflow with visual testsapps/frontend/playwright-visual.config.ts- Playwright visual test configapps/frontend/e2e/visual-regression.spec.ts- Visual regression test specs