Prompt
How do I automate regression tests for a frontend app in CI?
Latest observation
A practical way to automate frontend regression tests in CI is to split them into a few layers and run the right ones on every change.
1) Use a test pyramid
Typical setup:
- Unit tests: fast, run on every commit
- Component tests: render UI pieces in isolation
- End-to-end (E2E) tests: verify critical user flows in a real browser
- Visual regression tests: catch layout/style changes by comparing screenshots
For frontend regression, the most valuable CI checks are usually:
- unit/component tests for broad coverage
- a small number of E2E tests for critical paths
- visual tests for important pages/components
2) Pick the right tools
Common choices:
- Unit/component: Jest, Vitest, React Testing Library, Cypress Component Testing, Playwright Component Tests
- E2E: Playwright or Cypress
- Visual regression: Playwright screenshots, Percy, Chromatic, Applitools
If you want one tool that covers a lot, Playwright is a strong choice because it handles E2E well and can also do screenshot-based regression testing.
3) Make tests deterministic
Regression tests fail in CI mainly because they’re flaky. Reduce that by:
- using fixed test data/mocks
- isolating external APIs
- resetting DB/state before tests
- avoiding time-sensitive assertions unless you control time
- waiting for UI states instead of arbitrary sleeps
- setting consistent viewport, locale, timezone, and fonts
- disabling animations in tests
4) Add a CI pipeline
A typical CI workflow:
- Install dependencies
- Lint and type-check
- Run unit/component tests
- Build the app
- Start the app or preview environment
- Run E2E tests
- Run visual regression checks
- Upload artifacts: screenshots, videos, traces, reports
5) Example CI flow
Example with GitHub Actions:
name: frontend-ci
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Type check
run: npm run typecheck
- name: Unit tests
run: npm test -- --ci
- name: Build
run: npm run build
- name: Install Playwright browsers
run: npx playwright install --with-deps
- name: Run E2E tests
run: npm run test:e2e
- name: Upload Playwright report
if: failure()
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: playwright-report/
6) Add visual regression
If using Playwright, you can capture screenshots and compare them in CI.
Example test:
import { test, expect } from '@playwright/test';
test('home page matches screenshot', async ({ page }) => {
await page.goto('http://localhost:3000');
await expect(page).toHaveScreenshot('home.png');
});
Then:
- run once to generate baseline screenshots
- store baselines in git or a dedicated service
- fail CI if screenshots differ unexpectedly
For larger teams, tools like Percy or Chromatic make review of visual diffs easier.
7) Run only relevant tests when possible
To keep CI fast:
- run all checks on main branches
- on PRs, run only tests affected by changed files if your setup supports it
- split E2E tests into shards or parallel jobs
- run smoke tests first, full suite later
8) Make failures easy to debug
Have CI save:
- screenshots
- video recordings
- browser traces
- HTML test reports
- logs from backend mocks or test servers
Playwright is especially good here with trace viewer support.
9) Keep the test suite healthy
Maintain tests by:
- removing brittle selectors
- preferring roles/labels over CSS selectors
- keeping E2E tests focused on user journeys
- reviewing flaky tests immediately
- updating baselines intentionally for visual diffs
Recommended setup
If you’re starting fresh:
- Vitest + React Testing Library for unit/component tests
- Playwright for E2E and visual regression
- GitHub Actions or your CI provider to run everything on pull requests
If you want, I can give you:
- a complete GitHub Actions config,
- a Playwright setup for regression tests,
- or a step-by-step strategy for React/Vue/Angular specifically.