Testing - Visual Regression
Visual Regression Testing
How to test variable changes with visual regression testing.
Visual regression testing
Screenshot comparison
Compare screenshots before and after variable changes:
- Take screenshot of component with old variables
- Update variables
- Take screenshot of component with new variables
- Compare screenshots
- Flag differences
Mode switching tests
Test mode switching visually:
- Take screenshot in light mode
- Switch to dark mode
- Take screenshot in dark mode
- Compare mode differences
- Verify mode switching works
Component testing
Test individual components
Test components with variable changes:
- Render component with variables
- Take screenshot
- Update variables
- Re-render component
- Compare screenshots
Test component states
Test component states:
- Default state
- Hover state
- Focus state
- Disabled state
- Error state
Tools
Playwright
Use Playwright for visual regression:
import { test, expect } from "@playwright/test";
test("button visual regression", async ({ page }) => {
await page.goto("/button");
await expect(page).toHaveScreenshot("button.png");
});
Chromatic
Use Chromatic for component visual testing:
import { Button } from "./Button";
export default {
component: Button,
parameters: {
chromatic: { viewports: [320, 768, 1024] },
},
};
Percy
Use Percy for visual testing:
import percySnapshot from "@percy/playwright";
test("button visual test", async ({ page }) => {
await page.goto("/button");
await percySnapshot(page, "Button");
});
Implementation rules
- Test critical components
- Test all modes
- Test all states
- Use consistent viewports
- Review differences carefully
Failure modes
If visual regression testing fails:
- Visual changes go unnoticed
- Mode switching breaks
- Component states break
- Inconsistent styling
Out of scope
- Test framework setup (see tool docs)
- Component rendering (separate concern)
- Test infrastructure (separate concern)