| name | sfnext-testing |
| description | Write tests for Storefront Next using Vitest unit tests, Storybook stories, interaction tests, snapshot tests, and accessibility tests. Use when writing component tests, creating Storybook stories, running test coverage, or setting up test utilities. Covers testing-library patterns, play functions, and coverage thresholds. |
Testing Skill
This skill covers testing patterns in Storefront Next โ Vitest for unit tests and Storybook for component stories, interaction tests, and accessibility validation.
Unit Tests (Vitest)
Test files live alongside source files with .test.ts or .test.tsx extension:
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { ProductCard } from './product-card';
import { mockProduct } from '@/test-utils/mocks';
describe('ProductCard', () => {
it('renders product name', () => {
render(<ProductCard product={mockProduct} />);
expect(screen.getByText(mockProduct.productName)).toBeInTheDocument();
});
});
Test Utilities
Shared utilities in src/test-utils/:
config.ts โ Mock configuration objects and ConfigProvider wrappers
context-provider-utils.ts โ Context provider helpers for testing
context-provider.tsx โ Test context providers
Running Tests
pnpm test
pnpm test:ui
pnpm test:watch
Coverage Requirements
Thresholds enforced in vitest.thresholds.ts:
| Metric | Minimum |
|---|
| Lines | 73% |
| Statements | 73% |
| Functions | 72% |
| Branches | 67% |
Testing Libraries
@testing-library/react โ Component rendering and queries
@testing-library/jest-dom โ Custom DOM matchers (toBeInTheDocument, etc.)
@testing-library/user-event โ User interaction simulation
@vitest/coverage-v8 โ Code coverage
@vitest/ui โ Interactive test UI
Storybook
Every reusable component should have a .stories.tsx file.
Story Structure
import type { Meta, StoryObj } from '@storybook/react-vite';
import { within, expect } from 'storybook/test';
import { waitForStorybookReady } from '@storybook/test-utils';
import { ProductCard } from './product-card';
import { ConfigProvider } from '@/config/context';
import { mockConfig } from '@/test-utils/config';
import { mockProduct } from '@/test-utils/mocks';
const meta: Meta<typeof ProductCard> = {
title: 'Components/ProductCard',
component: ProductCard,
tags: ['autodocs', 'interaction'],
decorators: [
(Story) => (
<ConfigProvider config={mockConfig}>
<Story />
</ConfigProvider>
),
],
};
meta;
= < >;
: = {
: {
: mockProduct,
},
: ({ canvasElement }) => {
(canvasElement);
canvas = (canvasElement);
(canvas.(mockProduct.)).();
},
};
See Storybook Patterns Reference for advanced patterns.
Storybook Commands
pnpm storybook
pnpm build-storybook
pnpm test-storybook:snapshot
pnpm test-storybook:snapshot:update
pnpm test-storybook:interaction
pnpm test-storybook:a11y
pnpm generate:story-tests:coverage
Story Tags
| Tag | Purpose |
|---|
autodocs | Enable automatic documentation |
interaction | Include in interaction test runs |
skip-a11y | Exclude from a11y tests (use sparingly) |
Route Testing
Mock loaders, actions, and React Router context:
import { describe, test, expect, vi } from 'vitest';
import { render } from '@testing-library/react';
vi.mock('@/components/product-view', () => ({
default: ({ product }: any) => (
<div data-testid="product-view">
<div data-testid="product-name">{product?.name}</div>
</div>
),
}));
Test Organization
src/
โโโ components/
โ โโโ product-card/
โ โ โโโ index.tsx # Component
โ โ โโโ index.test.tsx # Unit tests
โ โ โโโ stories/
โ โ โโโ index.stories.tsx # Stories
โโโ routes/
โ โโโ _app.product.$productId.tsx
โ โโโ _app.product.$productId.test.tsx
โโโ test-utils/ # Shared utilities
โโโ config.ts
โโโ context-provider-utils.ts
Best Practices
- Colocate tests โ Keep test files next to source files
- Use test utilities โ Leverage
@/test-utils for mocks and providers
- Mock external dependencies โ Use
vi.mock() for API clients and context
- Test interactions โ Use
@testing-library/user-event and Storybook play functions
- Test accessibility โ Use Storybook a11y addon
- Multiple story variants โ Create stories for Default, Loading, Error states
- Use viewport toolbar โ Test responsive layouts via Storybook's built-in viewport toolbar
Related Skills
storefront-next:sfnext-components - Component patterns that need testing
storefront-next:sfnext-data-fetching - Mocking loaders and actions in tests
storefront-next:sfnext-page-designer - Testing Page Designer components
Reference Documentation