Lab 12: CSS Testing

Time: 30 minutes | Level: Advanced | Docker: docker run -it --rm node:20-alpine sh

Overview

Implement CSS testing strategies: Jest + jsdom for computed style tests, Testing Library for DOM queries, BackstopJS visual regression config, Storybook component states, and Percy/Chromatic concepts.


Step 1: CSS Testing Philosophy

Testing pyramid for CSS:
────────────────────────────────
Unit Tests      — class name generation, token values
Integration     — component renders correct classes
Visual Regression — screenshot diffs (Percy, Chromatic)
Manual/Axe      — accessibility validation

Tools by category:
─────────────────────────────────────────────
Tool              | Type      | What it tests
──────────────────────────────────────────────
Jest + jsdom      | Unit/Int  | Computed styles, class names
@testing-library  | Int       | DOM queries, user interactions
BackstopJS        | Visual    | Screenshot regression
Percy             | Visual    | PR screenshot diffs (CI)
Chromatic         | Visual    | Storybook story snapshots
Storybook         | Manual    | Component visual states
axe-core          | A11y      | WCAG compliance
stylelint         | Lint      | CSS code quality

Step 2: Jest + jsdom — Computed Style Tests


Step 3: @testing-library/dom


Step 4: BackstopJS Visual Regression


Step 5: Storybook for Component States


Step 6: Percy CI Integration


Step 7: CSS Custom Properties Testing


Step 8: Capstone — Jest + jsdom CSS Computed Style Test

📸 Verified Output:


Summary

Tool
Type
Command

Jest + jsdom

Unit/Integration

jest

@testing-library/dom

Integration

getByRole(), getByText()

BackstopJS

Visual regression

backstop test

Percy

CI visual diffs

percy exec

Chromatic

Storybook + visual

chromatic

Storybook

Component catalog

storybook dev

axe-core

Accessibility

axe.run(document)

stylelint

CSS lint

stylelint "**/*.css"

Last updated