This project uses Playwright Component Testing for visual regression testing and component validation.
npm run test:ctnpm run test:ct:uinpm run test:ct:updatenpm run test:server:unitComponent tests are located alongside components in __tests__ directories with .spec.tsx extension.
import {test, expect} from '@playwright/experimental-ct-react';
import {MyComponent} from '../MyComponent';
test.describe('MyComponent', () => {
test('should render correctly', async ({mount}) => {
const component = await mount(<MyComponent />);
await expect(component).toBeVisible();
});
test('should match screenshot', async ({mount}) => {
const component = await mount(<MyComponent />);
await expect(component).toHaveScreenshot('my-component.png');
});
});Screenshot tests create visual baselines that are compared on subsequent runs:
- First run - Creates baseline screenshots in
__snapshots__directories - Subsequent runs - Compares against baselines and fails if differences detected
- Update baselines - Run
npm run test:ct:updatewhen visual changes are intentional
- Keep screenshots focused on single components
- Test different states (default, hover, disabled, etc.)
- Test responsive behavior with different viewport sizes
- Name screenshots descriptively
src/
├── Component/
│ ├── Component.tsx
│ └── __tests__/
│ ├── Component.test.ts # Unit tests (Jest)
│ ├── Component.spec.tsx # Component tests (Playwright)
│ └── __snapshots__/
│ └── Component.spec.tsx-snapshots/
│ └── component-default-chromium.png
When a test fails due to screenshot mismatch:
- Check the test report:
playwright-report/index.html - Review the diff images showing:
- Expected (baseline)
- Actual (current)
- Diff (highlighted differences)
- If changes are intentional, update baselines with
npm run test:ct:update
Playwright configuration is in playwright-ct.config.ts:
- Test directory:
./src - Test pattern:
**/*.spec.tsx - Snapshot directory:
./__snapshots__ - Timeout: 10 seconds per test
- Browser: Chromium (Desktop Chrome)
For a quick reference of Playwright commands, see PLAYWRIGHT.md.