← ClaudeAtlas

testing-anti-patternslisted

Use when writing or changing tests, adding mocks, or tempted to add test-only methods to production code - prevents testing mock behavior, production pollution with test-only methods, and mocking without understanding dependencies
aiskillstore/marketplace · ★ 329 · Testing & QA · score 79
Install: claude install-skill aiskillstore/marketplace
# Testing Anti-Patterns ## Overview Tests must verify real behavior, not mock behavior. Mocks are a means to isolate, not the thing being tested. **Core principle:** Test what the code does, not what the mocks do. **Following strict TDD prevents these anti-patterns.** ## The Iron Laws ``` 1. NEVER test mock behavior 2. NEVER add test-only methods to production classes 3. NEVER mock without understanding dependencies ``` ## Anti-Pattern 1: Testing Mock Behavior **The violation:** ```typescript // ❌ BAD: Testing that the mock exists test('renders sidebar', () => { render(<Page />); expect(screen.getByTestId('sidebar-mock')).toBeInTheDocument(); }); ``` **Why this is wrong:** - You're verifying the mock works, not that the component works - Test passes when mock is present, fails when it's not - Tells you nothing about real behavior **your human partner's correction:** "Are we testing the behavior of a mock?" **The fix:** ```typescript // ✅ GOOD: Test real component or don't mock it test('renders sidebar', () => { render(<Page />); // Don't mock sidebar expect(screen.getByRole('navigation')).toBeInTheDocument(); }); // OR if sidebar must be mocked for isolation: // Don't assert on the mock - test Page's behavior with sidebar present ``` ### Gate Function ``` BEFORE asserting on any mock element: Ask: "Am I testing real component behavior or just mock existence?" IF testing mock existence: STOP - Delete the assertion or unmock the component Test