Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Testing – Visual Regress | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 49 of 50

·css

CSS Testing – Visual Regression & Linting

8 mins

Course progress98%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Run `stylelint "src/**/*.scss"` as part of `npm test`.
  • Visual regression tools capture screenshots of components and compare against a baseline.
  • Keep a baseline updated only after intentional design changes.

Testing CSS ensures consistency, standards compliance, and maintainability across the team.

1️⃣ Linting – Stylelint

  • Detect syntax errors, disallowed properties, or naming‑scheme violations.
  • Integrate with CI so a failing lint blocks a merge.

Sample .stylelintrc.json

{
  "extends": "stylelint-config-standard",
  "plugins": ["stylelint-order"],
  "rules": {
    "color-no-invalid-hex": true,
    "max-nesting-depth": 3,
    "order/order": [
      "custom-properties",
      "declarations"
    ]
  }
}

Run with npm run lint:css.

2️⃣ Visual Regression – BackstopJS / Percy / Chromatic

  • Capture screenshots of components/pages.
  • Compare new builds against a baseline; any pixel difference flags a regression.

BackstopJS Quick Setup

npm install -g backstopjs
backstop init   # creates config
backstop test   # runs visual regression

Configure viewports, selectors, and delay (for animations).

3️⃣ Accessibility Audits

  • Lighthouse (Chrome DevTools) > Accessibility panel.
  • axe-core – CLI (npx axe) or browser extension.
  • Automated tests can be part of the CI pipeline (e.g., npm run test:axe).

4️⃣ Performance Checks

  • Run Lighthouse > Performance to catch heavy CSS paint/layout.
  • Use Chrome DevTools → Coverage tab to see unused CSS after a page load.

CI Example (GitHub Actions)

name: CSS CI
on: [push, pull_request]
jobs:
  lint-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install deps
        run: npm ci
      - name: Lint CSS
        run: npm run lint:css
      - name: Visual Regression (Backstop)
        run: npm run backstop:test

Pro Tip – Keep a dedicated folder for visual baselines (/tests/baselines). Commit them to version control; when a UI change is intentional, update the baseline with backstop approve.


Accessibility of Tests

  • Lint rules should enforce focus-visible and color contrast.
  • Images in the test pages must have alt attributes; otherwise visual diff tools can’t verify semantics.

Real‑World Workflow

  1. Write component + CSS.
  2. Run stylelint locally – fix any warnings.
  3. Run visual regression locally (backstop test).
  4. Commit changes; CI runs the same steps, guaranteeing no regressions reach production.

Common Pitfalls

SymptomCauseFix
Random failures on CIDifferent device pixel ratio (DPR) affecting screenshots.Set --web-push --chromeFlags="--force-device-scale-factor=1" in Backstop config.
Lint errors on vendor prefixesautoprefixer adds prefixes that the linter flags as unknown.Add "ignore: ['-webkit-']" rule or run lint after autoprefixing.

Final Thought

Combining automated linting with visual regression gives you a safety net: the linter catches syntax and convention bugs, while visual diff catches appearance regressions that static analysis can’t see.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS for Performance – Avoiding Layout ThrashContinue to lesson 50Capstone Project – Build a Complete Responsive SiteBack to CSS Basics to Advanced curriculum