Fixes failing or broken unit tests. Loads repo test patterns, diagnoses failures, applies minor fixes (mocks, assertions, snapshots). Surfaces structural issues for human review. Use when unit tests are failing after a code change.
Arguments: $ARGUMENTS (optional: path to specific test file or package)
Call test-context to get established mock patterns, factory conventions, and assertion style for this repo.
# Scope to argument if provided, otherwise run all unit tests
pnpm test ${ARGUMENTS:-""} --run 2>&1 | tee /tmp/test-output.txt
Parse failures: group by file, extract error message and line number.
For each failing test, read the test file and the source file it tests. Categorize:
| Category | Fix approach |
|---|---|
| Mock is missing a new method/property | Add to mock — match the real interface |
| Assertion uses old value (source changed) | Update assertion to match new behavior |
| Test data factory missing new required field | Add field with sensible test default |
| Snapshot out of date | Update snapshot if change is intentional |
| Import path changed | Update import |
| Test is testing a removed behavior | Delete the test with a comment explaining why |
| Test logic is wrong for the new behavior | Rewrite the test to reflect new behavior |
| Structural issue (test architecture is wrong) | Surface for human review — do not guess |
Do not:
// @ts-ignore or cast to any to suppress errorsApply fixes for all categorized failures. For each fix:
pnpm test ${ARGUMENTS:-""} --run 2>&1 | tail -20
Iterate until all fixable failures are resolved.
## Unit Test Cleanup Complete
Fixed ({count}):
- {test name}: {fix applied}
Surfaced for review ({count}):
- {test name}: {why it needs human decision}
Test run: {pass count} passing, {fail count} remaining
REPO=$(git remote get-url origin 2>/dev/null | sed 's/.*\///' | sed 's/\.git//')
BRANCH=$(git branch --show-current 2>/dev/null | sed 's/\//-/g')
TS=$(date +%Y%m%d-%H%M%S)-$$
mkdir -p ~/.claude/skill-output/$REPO/$BRANCH
Write this report to ~/.claude/skill-output/$REPO/$BRANCH/cleanup-unit-tests-report-$TS.md.