/test:testing-standardsfrom test
Enforces testing standards including test pyramid, TDD, query priority, test isolation, naming conventions, and flaky-test handling. Use when writing, organizing, or reviewing tests.
Ships in test. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Rule Name: testing-standards Priority: High Applies To: commands, agents Enforces On: test:frontend-testing-engineer
Comprehensive testing standards with Context7-verified best practices for unit, integration, and E2E tests. This rule ensures all tests follow industry-standard patterns from official testing framework documentation.
MANDATORY: All testing implementations must reference these Context7 sources:
/vitest-dev/vitest - Vitest testing framework patterns/testing-library/testing-library-docs - Testing Library query patterns/testing-library/react-testing-library - React Testing Library best practices/jestjs/jest - Jest testing framework (when applicable)MANDATORY for all new features:
Red-Green-Refactor Cycle:
┌──────────────────────────────────────┐
│ 1. RED: Write failing test first │
│ ↓ │
│ 2. GREEN: Write minimum code to pass│
│ ↓ │
│ 3. REFACTOR: Improve code quality │
│ ↓ │
│ 4. REPEAT for next feature │
└──────────────────────────────────────┘
Example:
// ✅ CORRECT TDD Flow
// Step 1: Write failing test FIRST
test('should calculate total price', () => {
expect(calculateTotal([10, 20, 30])).toBe(60);
});
// Step 2: Implement minimum code
function calculateTotal(prices) {
return prices.reduce((sum, price) => sum + price, 0);
}
// Step 3: Refactor if needed
function calculateTotal(prices) {
if (!Array.isArray(prices)) throw new Error('Invalid input');
return prices.reduce((sum, price) => sum + price, 0);
}
MANDATORY: Follow Testing Library's query priority hierarchy:
// Priority 1: Accessible to Everyone (BEST)
// ✅ Queries accessible to screen readers and users
screen.getByRole('button', { name: 'Submit' })
screen.getByLabelText('Email address')
screen.getByPlaceholderText('Enter your name')
screen.getByText('Welcome back!')
screen.getByDisplayValue('current value')
// Priority 2: Semantic Queries (GOOD)
// ✅ Queries that match HTML semantics
screen.getByAltText('Profile picture')
screen.getByTitle('Close dialog')
// Priority 3: Test IDs (ACCEPTABLE)
// ⚠️ Only when semantic queries don't work
screen.getByTestId('custom-element')
// Priority 4: CSS Selectors (AVOID)
// ❌ Tied to implementation, breaks easily
container.querySelector('.submit-button') // DON'T USE
container.querySelector('#email-input') // DON'T USE
MANDATORY: Each test must be completely independent:
// ❌ WRONG: Tests depend on each other
let user = null;
test('creates user', () => {
user = { id: 1, name: 'John' };
expect(user).toBeTruthy();
});
test('updates user', () => {
user.name = 'Jane'; // Depends on previous test
expect(user.name).toBe('Jane');
});
// ✅ CORRECT: Independent tests
describe('User operations', () => {
beforeEach(() => {
// Fresh state for each test
mockDatabase.reset();
});
test('creates user', () => {
const user = createUser({ name: 'John' });
expect(user).toEqual({ id: 1, name: 'John' });
});
test('updates user', () => {
const user = createUser({ name: 'John' });
const updated = updateUser(user.id, { name: 'Jane' });
expect(updated.name).toBe('Jane');
});
});
MANDATORY: Use Testing Library's async utilities:
// ❌ WRONG: Manual delays
test('shows data after loading', async () => {
render(<DataDisplay />);
await new Promise(resolve => setTimeout(resolve, 1000));
expect(screen.getByText('Data loaded')).toBeInTheDocument();
});
// ✅ CORRECT: Async utilities with auto-retry
test('shows data after loading', async () => {
render(<DataDisplay />);
// Automatically waits and retries
expect(await screen.findByText('Data loaded')).toBeInTheDocument();
});
// ✅ CORRECT: waitFor for complex conditions
test('shows updated count', async () => {
render(<Counter />);
await userEvent.click(screen.getByRole('button', { name: 'Increment' }));
await waitFor(() => {
expect(screen.getByText('Count: 1')).toBeInTheDocument();
});
});
MANDATORY: Test behavior, not implementation:
// ❌ WRONG: Testing implementation details
test('sets loading state to true', () => {
const { result } = renderHook(() => useData());
expect(result.current.loading).toBe(true); // Internal state
});
// ✅ CORRECT: Testing user-visible behavior
test('shows loading indicator while fetching', () => {
render(<DataComponent />);
expect(screen.getByText('Loading...')).toBeInTheDocument();
});
// ❌ WRONG: Accessing component internals
test('updates state on click', () => {
const wrapper = shallow(<Component />);
wrapper.find('button').simulate('click');
expect(wrapper.state('clicked')).toBe(true);
});
// ✅ CORRECT: Testing user interactions
test('shows success message after submit', async () => {
const user = userEvent.setup();
render(<Form />);
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(await screen.findByText('Success!')).toBeInTheDocument();
});
MANDATORY for all tests:
test('user can log in with valid credentials', async () => {
// ARRANGE: Set up test data and environment
const user = userEvent.setup();
const credentials = { email: '[email protected]', password: 'pass123' };
render(<LoginForm />);
// ACT: Perform the action being tested
await user.type(screen.getByLabelText('Email'), credentials.email);
await user.type(screen.getByLabelText('Password'), credentials.password);
await user.click(screen.getByRole('button', { name: 'Log in' }));
// ASSERT: Verify expected outcome
expect(await screen.findByText('Welcome!')).toBeInTheDocument();
});
MANDATORY: Test names must describe behavior:
// ❌ WRONG: Vague test names
test('it works', () => { });
test('button', () => { });
test('user test', () => { });
// ✅ CORRECT: Descriptive behavior-focused names
test('should display error message when email is invalid', () => { });
test('should navigate to dashboard after successful login', () => { });
test('should disable submit button while form is submitting', () => { });
// ✅ CORRECT: Organized with describe blocks
describe('LoginForm', () => {
describe('when user provides valid credentials', () => {
test('should successfully log in', () => { });
test('should clear form fields', () => { });
test('should redirect to dashboard', () => { });
});
describe('when user provides invalid credentials', () => {
test('should display error message', () => { });
test('should keep user on login page', () => { });
test('should clear password field', () => { });
});
});
MANDATORY: All new code must meet these thresholds:
// vitest.config.js
export default {
test: {
coverage: {
thresholds: {
lines: 80, // Minimum 80% line coverage
functions: 80, // Minimum 80% function coverage
branches: 75, // Minimum 75% branch coverage
statements: 80 // Minimum 80% statement coverage
}
}
}
};
// ✅ CORRECT: Exclude non-testable code
coverage: {
exclude: [
'node_modules/',
'test/',
'**/*.config.js',
'**/*.spec.js',
'**/*.test.js',
'src/main.js', // App entry point
'src/index.js', // Entry point
'src/**/*.d.ts', // Type definitions
'src/constants.js', // Pure constants
'src/types.js' // Type exports
]
}
Coverage Checklist:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
❌ 100% coverage with poor tests:
- Tests that only check code executes
- No assertions about correctness
- Testing implementation details
- Brittle tests that break easily
✅ 80% coverage with quality tests:
- Tests verify correct behavior
- Tests check edge cases
- Tests use semantic queries
- Tests are maintainable
- Tests document expected behavior
💡 Focus on test quality, not just coverage numbers!
// ✅ CORRECT: Mock at network level with MSW
import { rest } from 'msw';
import { setupServer } from 'msw/node';
const server = setupServer(
rest.get('/api/user/:id', (req, res, ctx) => {
return res(ctx.json({ id: req.params.id, name: 'Test User' }));
})
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
test('fetches user data', async () => {
render(<UserProfile userId={1} />);
expect(await screen.findByText('Test User')).toBeInTheDocument();
});
// ✅ CORRECT: Mock external dependencies
import { vi } from 'vitest';
vi.mock('../services/analytics', () => ({
trackEvent: vi.fn(),
trackPageView: vi.fn()
}));
test('tracks button click', async () => {
const { trackEvent } = await import('../services/analytics');
render(<Button />);
await userEvent.click(screen.getByRole('button'));
expect(trackEvent).toHaveBeenCalledWith('button_click');
});
MANDATORY for UI components:
import { axe, toHaveNoViolations } from 'jest-axe';
expect.extend(toHaveNoViolations);
test('should have no accessibility violations', async () => {
const { container } = render(<MyComponent />);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
// ✅ Test keyboard navigation
test('should be keyboard navigable', async () => {
const user = userEvent.setup();
render(<Form />);
// Tab through form fields
await user.tab();
expect(screen.getByLabelText('Name')).toHaveFocus();
await user.tab();
expect(screen.getByLabelText('Email')).toHaveFocus();
});
// ✅ Test screen reader text
test('should have descriptive aria-label', () => {
render(<IconButton icon="close" />);
expect(screen.getByRole('button', { name: 'Close dialog' }))
.toBeInTheDocument();
});
// ✅ Test component rendering performance
test('should render large list efficiently', () => {
const items = Array.from({ length: 10000 }, (_, i) => ({ id: i }));
const start = performance.now();
render(<VirtualizedList items={items} />);
const duration = performance.now() - start;
expect(duration).toBeLessThan(100); // Should render in < 100ms
});
// ✅ Use Vitest's benchmark feature
import { bench, describe } from 'vitest';
describe('Array operations', () => {
bench('Array.push', () => {
const arr = [];
for (let i = 0; i < 1000; i++) {
arr.push(i);
}
});
bench('Array spread', () => {
let arr = [];
for (let i = 0; i < 1000; i++) {
arr = [...arr, i];
}
});
});
// ❌ WRONG: Testing React itself
test('useState works', () => {
const { result } = renderHook(() => useState(0));
expect(result.current[0]).toBe(0); // Don't test React
});
// ✅ CORRECT: Test your component behavior
test('counter increments when button clicked', async () => {
render(<Counter />);
await userEvent.click(screen.getByRole('button', { name: 'Increment' }));
expect(screen.getByText('Count: 1')).toBeInTheDocument();
});
// ❌ WRONG: Large snapshots that break often
test('renders correctly', () => {
const { container } = render(<ComplexComponent />);
expect(container).toMatchSnapshot(); // 500 line snapshot
});
// ✅ CORRECT: Targeted snapshots for specific elements
test('renders error message correctly', () => {
render(<Form error="Invalid input" />);
const errorEl = screen.getByRole('alert');
expect(errorEl).toMatchInlineSnapshot(`
<div role="alert" class="error">
Invalid input
</div>
`);
});
// ❌ WRONG: Tests depend on execution order
test.only('test 1', () => { sharedState = 'value'; });
test('test 2', () => { expect(sharedState).toBe('value'); });
// ✅ CORRECT: Tests are independent
beforeEach(() => {
sharedState = getInitialState();
});
test('test 1', () => { /* uses sharedState */ });
test('test 2', () => { /* uses sharedState */ });
Before committing test code, verify:
Run these checks before merge:
# Run all tests
npm test
# Check coverage
npm run test:coverage
# Run accessibility tests
npm run test:a11y
# Run in CI mode
npm run test:ci
test-coverage-requirements - Coverage threshold enforcementaccessibility-standards - A11y testing requirementscode-quality-standards - General code quality rulesFor questions about testing standards:
test/examples/What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
testing-standards, tdd, test-pyramid, test-isolation, flaky-tests
npx claudepluginhub lagowski/claudeautopm --plugin test2 skills in this plugin
Other plugins' skills for the same job