$npx -y skills add jamditis/claude-skills-journalism --skill test-first-bugsThis skill should be used when the user reports a bug, describes unexpected behavior, says something is "broken", "not working", "failing", mentions an "error", "issue", or "problem" in code, or asks to "fix" something. Enforces test-driven bug fixing workflow.
| 1 | # Test-first bug fixing |
| 2 | |
| 3 | Enforce a disciplined bug-fixing workflow that prevents regression and parallelizes fix attempts. |
| 4 | |
| 5 | ## Core workflow |
| 6 | |
| 7 | When a bug is reported, follow these steps in order: |
| 8 | |
| 9 | ### Phase 1: Reproduce and document |
| 10 | |
| 11 | 1. **Understand the bug** — Gather details about expected vs actual behavior |
| 12 | 2. **Identify the test location** — Determine where tests live in the project (check for `tests/`, `__tests__/`, `spec/`, `*.test.*`, `*.spec.*` patterns) |
| 13 | 3. **Write a failing test** — Create a test that demonstrates the bug |
| 14 | |
| 15 | ### Phase 2: Fix with subagents |
| 16 | |
| 17 | 4. **Launch fix subagents** — Use the Task tool with `subagent_type=general-purpose` to attempt fixes |
| 18 | 5. **Run the test** — Verify the fix by running the specific test |
| 19 | 6. **Iterate if needed** — If test still fails, launch additional subagents with new approaches |
| 20 | |
| 21 | ### Phase 3: Verify and complete |
| 22 | |
| 23 | 7. **Run full test suite** — Ensure no regressions were introduced |
| 24 | 8. **Report success** — Confirm the bug is fixed with passing test as proof |
| 25 | |
| 26 | ## Writing the failing test |
| 27 | |
| 28 | ### Test naming convention |
| 29 | |
| 30 | Name the test to describe the bug: |
| 31 | |
| 32 | ```python |
| 33 | # Python (pytest) |
| 34 | def test_user_login_fails_when_email_has_uppercase(): |
| 35 | ... |
| 36 | |
| 37 | # Python (unittest) |
| 38 | def test_should_handle_empty_input_without_crashing(self): |
| 39 | ... |
| 40 | ``` |
| 41 | |
| 42 | ```javascript |
| 43 | // JavaScript (Jest/Vitest) |
| 44 | it('should not crash when input array is empty', () => { ... }); |
| 45 | test('handles special characters in username', () => { ... }); |
| 46 | ``` |
| 47 | |
| 48 | ```typescript |
| 49 | // TypeScript |
| 50 | describe('UserService', () => { |
| 51 | it('returns null when user not found instead of throwing', () => { ... }); |
| 52 | }); |
| 53 | ``` |
| 54 | |
| 55 | ### Test structure |
| 56 | |
| 57 | Every bug reproduction test follows this pattern: |
| 58 | |
| 59 | ```python |
| 60 | def test_bug_description(): |
| 61 | # 1. ARRANGE - Set up the conditions that trigger the bug |
| 62 | input_data = create_problematic_input() |
| 63 | |
| 64 | # 2. ACT - Perform the action that causes the bug |
| 65 | result = function_under_test(input_data) |
| 66 | |
| 67 | # 3. ASSERT - Verify the expected (correct) behavior |
| 68 | assert result == expected_value # This should FAIL initially |
| 69 | ``` |
| 70 | |
| 71 | ### Finding the right test file |
| 72 | |
| 73 | Check the project structure for existing test patterns: |
| 74 | |
| 75 | ```bash |
| 76 | # Find test files |
| 77 | find . -name "*.test.*" -o -name "*.spec.*" -o -name "test_*.py" | head -20 |
| 78 | |
| 79 | # Find test directories |
| 80 | ls -la tests/ __tests__/ spec/ test/ 2>/dev/null |
| 81 | |
| 82 | # Check package.json for test command |
| 83 | grep -A5 '"test"' package.json |
| 84 | ``` |
| 85 | |
| 86 | ## Launching fix subagents |
| 87 | |
| 88 | Use the Task tool to parallelize fix attempts: |
| 89 | |
| 90 | ``` |
| 91 | Task tool parameters: |
| 92 | - subagent_type: "general-purpose" |
| 93 | - description: "Fix [bug description]" |
| 94 | - prompt: Include: |
| 95 | 1. The bug description |
| 96 | 2. The failing test location and contents |
| 97 | 3. Suspected cause (if known) |
| 98 | 4. Constraint: "Run the test to verify your fix works" |
| 99 | ``` |
| 100 | |
| 101 | ### Parallel fix strategies |
| 102 | |
| 103 | Launch multiple subagents with different approaches: |
| 104 | |
| 105 | 1. **Direct fix agent** — Focus on the immediate code causing the bug |
| 106 | 2. **Root cause agent** — Investigate deeper architectural issues |
| 107 | 3. **Edge case agent** — Look for similar bugs in related code |
| 108 | |
| 109 | ## When projects lack tests |
| 110 | |
| 111 | If the project has no test infrastructure: |
| 112 | |
| 113 | 1. **Set up minimal test framework** first |
| 114 | 2. **Create the test file** in a sensible location |
| 115 | 3. **Document the test setup** for future use |
| 116 | |
| 117 | ### Quick test setup commands |
| 118 | |
| 119 | ```bash |
| 120 | # Python |
| 121 | pip install pytest |
| 122 | mkdir -p tests && touch tests/__init__.py |
| 123 | |
| 124 | # JavaScript/TypeScript |
| 125 | npm install --save-dev jest |
| 126 | # or |
| 127 | npm install --save-dev vitest |
| 128 | |
| 129 | # Go |
| 130 | # Tests are built-in, create *_test.go files |
| 131 | ``` |
| 132 | |
| 133 | ## Verifying the fix |
| 134 | |
| 135 | After subagent reports completion: |
| 136 | |
| 137 | ```bash |
| 138 | # Run the specific test |
| 139 | pytest tests/test_module.py::test_bug_description -v |
| 140 | npm test -- --grep "bug description" |
| 141 | go test -run TestBugDescription -v |
| 142 | |
| 143 | # Run full suite to check for regressions |
| 144 | pytest |
| 145 | npm test |
| 146 | go test ./... |
| 147 | ``` |
| 148 | |
| 149 | ## Example workflow |
| 150 | |
| 151 | **User reports:** "The login function crashes when email has spaces" |
| 152 | |
| 153 | **Phase 1 — Write failing test:** |
| 154 | ```python |
| 155 | # tests/test_auth.py |
| 156 | def test_login_handles_email_with_spaces(): |
| 157 | """Bug: Login crashes when email contains spaces""" |
| 158 | auth = AuthService() |
| 159 | |
| 160 | # This should return an error, not crash |
| 161 | result = auth.login("user @example.com", "password") |
| 162 | |
| 163 | assert result.success == False |
| 164 | assert "invalid email" in result.error.lower() |
| 165 | ``` |
| 166 | |
| 167 | **Run test to confirm it fails:** |
| 168 | ```bash |
| 169 | pytest tests/test_auth.py::test_login_handles_email_with_spaces -v |
| 170 | # Expected: FAILED (demonstrates the bug) |
| 171 | ``` |
| 172 | |
| 173 | **Phase 2 — Launch subagent:** |
| 174 | ``` |
| 175 | Task tool: |
| 176 | - subagent_type: "general-purpose" |
| 177 | - description: "Fix email space crash" |
| 178 | - prompt: "Fix the login crash when email contains spaces. |
| 179 | |
| 180 | Bug: AuthService.login() crashes instead of returning error when email has spaces. |