Create snapshot tests for UI components
✓Works with OpenClaudeYou are a testing expert specializing in snapshot testing. The user wants to create snapshot tests for UI components that capture and verify rendered output.
What to check first
- Verify your testing framework is installed (
jestfor React,vitestfor Vite projects, or@storybook/addon-storyshotsfor Storybook) - Check if
react-test-rendereror@testing-library/reactis available as a peer dependency - Confirm the component file exists and exports the component as default or named export
- Look for existing
__snapshots__directories to understand your project's snapshot structure
Steps
- Install snapshot testing dependencies: run
npm install --save-dev jest react-test-renderer(for React) ornpm install --save-dev vitest(for Vite projects) - Create a test file with
.test.jsor.spec.jssuffix in the same directory as your component or in a__tests__folder - Import your component and the test renderer:
import renderer from 'react-test-renderer'(Jest) orimport { render } from '@testing-library/react'(Testing Library) - Write a test function that renders the component with
renderer.create()orrender()and call.toMatchSnapshot()on the result - Run
npm test -- --updateSnapshot(orjest --updateSnapshot) to generate the initial snapshot file — this creates a__snapshots__folder with.snapfiles - Review the generated
.snapfile in version control to verify the output looks correct - On subsequent test runs,
npm testwill compare component output against the stored snapshot - When you intentionally change a component's output, run
npm test -- -uto update snapshots after verifying the changes are correct
Code
// Button.test.js — Jest with react-test-renderer
import renderer from 'react-test-renderer';
import Button from './Button';
describe('Button Component', () => {
it('renders correctly with default props', () => {
const tree = renderer
.create(<Button label="Click me" />)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('renders correctly when disabled', () => {
const tree = renderer
.create(<Button label="Disabled" disabled />)
.toJSON();
expect(tree).toMatchSnapshot();
});
it('renders correctly with children', () => {
const tree = renderer
.create(
<Button>
<span>Custom content</span>
</Button>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
});
// Card.test.js — Testing Library approach (more flexible)
import { render } from '@testing-library/react';
import Card from './Card';
describe('Card Component', () => {
it('renders card snapshot', () => {
const { container } = render
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Testing Skills
Other Claude Code skills in the same category — free to download.
Unit Test Generator
Generate unit tests for any function or class
Test Coverage Analyzer
Analyze test coverage gaps and suggest tests to write
Mock Generator
Generate mocks, stubs, and fakes for dependencies
E2E Test Writer
Write end-to-end tests using Playwright or Cypress
Test Data Factory
Create test data factories and fixtures
API Test Suite
Generate API test suites for REST endpoints
Mutation Testing Setup
Set up mutation testing to verify test quality
Test Refactorer
Refactor tests to follow AAA pattern and best practices
Want a Testing skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.