prioritize-unit-testing-and-refactor-for-testability
Installation
SKILL.md
Summary
Focus your testing efforts on the base of the Test Pyramid: Unit Tests. If code is hard to unit test, do not rely on slow integration tests; instead, refactor the code by extracting logic into smaller, testable functions (refer to the Refactoring Guidelines, such as "Keep functions short").
Rationale
- Speed: Unit tests run in milliseconds, providing immediate feedback. Integration tests are slow and brittle.
- Design Feedback: "Hard to test" usually means "Bad design" (tight coupling, hidden dependencies).
- Coverage: It is impossible to cover all edge cases (permutations) with expensive E2E/Integration tests. Only Unit tests can cheaply cover all branches.
Guidance
- Refactor to Test:
- If a function is too complex to test easily, extract the logic into helper functions.
- Follow the Refactoring Guidelines (e.g. "Keep Functions Short" / Top-Down Decomposition) when extracting. Do not split arbitrarily.
- Encapsulation with Internals:
- Move extracted helper functions to an
internal/package (Go) or directory (TS/JS) if they shouldn't be part of the public API. - Test these
internalunits directly to verify complex logic in isolation.
- Move extracted helper functions to an
- Pyramid Shape:
- 70% Unit Tests: Fast, isolated, covers all edge cases.
- 20% Integration Tests: Covers valid paths and major error cases across boundaries.
- 10% E2E Tests: Covers critical user flows ("Happy Path").