The astro-basics project uses Storybook for interactive component development and Chromatic for automated visual regression testing. Together, they provide a complete solution for testing and documenting React components.
Overview
Section titled “Overview”📚 Storybook
Develop and test React components in isolation with interactive controls and documentation.
🎨 Chromatic
Automated visual regression testing that catches unintended UI changes before they ship.
♿ Accessibility
Built-in a11y addon for WCAG compliance checking on every component.
🔄 CI Integration
Seamless GitHub Actions integration for automated testing on every PR.
Quick Start
Section titled “Quick Start”Running Storybook
Section titled “Running Storybook”# Start Storybook development servernpm run storybook
# Opens at http://localhost:6006Building Static Storybook
Section titled “Building Static Storybook”# Build for deploymentnpm run build-storybook
Running Chromatic
Section titled “Running Chromatic”# Run visual regression tests (loads .env automatically)npm run chromatic
# Override token for one-off runsCHROMATIC_PROJECT_TOKEN=<your-token> npm run chromaticStorybook Configuration
Section titled “Storybook Configuration”Project Structure
Section titled “Project Structure”Stories are co-located with their components:
src/components/react/├── Alert.tsx├── Alert.stories.tsx # Story file├── RoleBadge.tsx└── RoleBadge.stories.tsx # Story filePath Aliases
Section titled “Path Aliases”Storybook is configured to support the project’s # path aliases:
viteFinal: async (config) => { config.resolve.alias = { '#components': new URL('../src/components', import.meta.url).pathname, '#utils': new URL('../src/utils', import.meta.url).pathname, '#libs': new URL('../src/libs', import.meta.url).pathname, '#types': new URL('../src/types', import.meta.url).pathname, '#constants': new URL('../src/constants', import.meta.url).pathname, } return config}Installed Addons
Section titled “Installed Addons”| Addon | Purpose |
|---|---|
@storybook/addon-essentials | Docs, controls, actions, viewport, backgrounds |
@storybook/addon-interactions | Testing component interactions |
@storybook/addon-a11y | Accessibility testing |
Writing Stories
Section titled “Writing Stories”Basic Story Structure
Section titled “Basic Story Structure”import type { Meta, StoryObj } from '@storybook/react'import Alert from './Alert'
const meta = { title: 'Components/Alert', component: Alert, parameters: { layout: 'centered', }, tags: ['autodocs'], argTypes: { type: { control: { type: 'select' }, options: ['error', 'success', 'info'], description: 'The visual style of the alert', }, },} satisfies Meta<typeof Alert>
export default metatype Story = StoryObj<typeof meta>
export const Error: Story = { args: { type: 'error', children: 'This is an error message.', },}export const Success: Story = { args: { type: 'success', children: 'Operation completed successfully!', },}
export const Info: Story = { args: { type: 'info', children: 'Here is some helpful information.', },}
export const LongContent: Story = { args: { type: 'info', children: 'This is a longer message that demonstrates how the component handles more content.', },}export const AllVariants: Story = { render: () => ( <div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}> <Alert type="error">Error alert</Alert> <Alert type="success">Success alert</Alert> <Alert type="info">Info alert</Alert> </div> ),}Story Best Practices
Section titled “Story Best Practices”Essential patterns:
- Use
satisfies Meta<typeof Component>for type safety - Add
tags: ['autodocs']for automatic documentation - Define
argTypesfor interactive controls - Create multiple stories for different states
- Use
layout: 'centered'for most components
Existing Stories
Section titled “Existing Stories”The project includes stories for these components:
| Component | File | Stories |
|---|---|---|
| Alert | Alert.stories.tsx | Error, Success, Info, LongContent |
| RoleBadge | RoleBadge.stories.tsx | Member, Volunteer, TeamManager, TeamAdmin, Admin, SuperAdmin, AllRoles |
Chromatic Visual Testing
Section titled “Chromatic Visual Testing”-
Create a Chromatic account at chromatic.com
-
Link your repository and create a new project
-
Copy your project token from the Chromatic dashboard
-
Add the token to
.env(loaded automatically):Terminal window CHROMATIC_PROJECT_TOKEN=<your-token> -
Run Chromatic (extra flags forwarded):
Terminal window npm run chromatic
How Chromatic Works
Section titled “How Chromatic Works”1. Build
Chromatic builds your Storybook and captures screenshots of every story.
2. Compare
Screenshots are compared against baselines to detect visual changes.
3. Review
Changes are flagged for review in the Chromatic dashboard.
4. Approve
Accept intentional changes or flag regressions for fixing.
CI/CD Integration
Section titled “CI/CD Integration”Add Chromatic to your GitHub Actions workflow:
name: Chromatic
on: push: branches: [primary] pull_request: branches: [primary]
jobs: chromatic: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0
- name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20' cache: 'npm'
- name: Install dependencies run: npm ci
- name: Run Chromatic uses: chromaui/action@latest with: projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }} exitZeroOnChanges: trueAccessibility Testing
Section titled “Accessibility Testing”The @storybook/addon-a11y addon provides automated accessibility checks:
Features
Section titled “Features”- WCAG 2.1 Level AA compliance checking
- axe-core powered analysis
- Real-time feedback in Storybook UI
- Violation highlighting on components
Using the A11y Panel
Section titled “Using the A11y Panel”- Open Storybook (
npm run storybook) - Navigate to any story
- Click the “Accessibility” tab in the addon panel
- Review violations, passes, and incomplete checks
Common A11y Issues
Section titled “Common A11y Issues”| Issue | Solution |
|---|---|
| Missing alt text | Add alt attribute to images |
| Low contrast | Adjust color combinations for 4.5:1 ratio |
| Missing labels | Add aria-label or visible labels |
| Focus indicators | Ensure visible :focus styles |
| Heading hierarchy | Use sequential heading levels (h1 → h2 → h3) |
Available Scripts
Section titled “Available Scripts”| Script | Description |
|---|---|
npm run storybook | Start dev server on port 6006 |
npm run build-storybook | Build static Storybook |
npm run chromatic | Run visual regression tests (loads .env, forwards extra flags) |
Troubleshooting
Section titled “Troubleshooting”Path Alias Errors
Section titled “Path Alias Errors”If you see errors like Cannot find module '#utils/...':
- Verify
.storybook/main.tshas correct alias configuration - Ensure paths match
tsconfig.jsonsettings - Restart Storybook after config changes
Component Not Rendering
Section titled “Component Not Rendering”Chromatic Build Failures
Section titled “Chromatic Build Failures”- Verify
CHROMATIC_PROJECT_TOKENis set - Test local build:
npm run build-storybook - Check for build errors in the output
- Review Chromatic dashboard for specific failures
Missing Stories
Section titled “Missing Stories”Ensure your story files:
- Are named
*.stories.tsxor*.stories.ts - Are located in
src/components/react/ - Export a default meta object
Best Practices
Section titled “Best Practices”- Co-locate stories with components
- Write stories for all variants and states
- Use autodocs for automatic documentation
- Test accessibility before merging
- Review Chromatic changes carefully
- Keep stories simple and focused
// ✅ Good - focused, typed, documentedconst meta = { title: 'Components/Button', component: Button, tags: ['autodocs'], argTypes: { variant: { control: 'select', options: ['primary', 'secondary'], }, },} satisfies Meta<typeof Button>❌ DON’T
Section titled “❌ DON’T”- Don’t skip accessibility checks - Fix violations before merging
- Don’t auto-accept Chromatic changes - Always review visually
- Don’t use Astro APIs in React components for stories
- Don’t forget to update stories when component APIs change
Related Resources
Section titled “Related Resources”E2E Testing
Project Documentation
Storybook Docs
Chromatic Docs
Summary
Section titled “Summary”The Storybook and Chromatic setup provides:
- 📚 Interactive development with hot reload and controls
- 🎨 Visual regression testing catching UI changes automatically
- ♿ Accessibility testing ensuring WCAG compliance
- 📝 Auto-generated docs from component props
- 🔄 CI/CD integration for automated testing on PRs
Key Takeaway: Write stories for all component variants, review Chromatic changes before merging, and fix accessibility violations early!