# test-driven-development > Use when implementing any feature or bugfix, before writing implementation code - Author: botarhythm - Repository: botarhythm/DegiHara-ChatBot - Version: 20260206143943 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/botarhythm/DegiHara-ChatBot - Web: https://mule.run/skillshub/@@botarhythm/DegiHara-ChatBot~test-driven-development:20260206143943 --- --- name: test-driven-development description: Use when implementing any feature or bugfix, before writing implementation code --- # Test-Driven Development (TDD) ## Overview Write the test first. Watch it fail. Write minimal code to pass. **Core principle:** If you didn't watch the test fail, you don't know if it tests the right thing. ## When to Use **Always:** - New features - Bug fixes - Refactoring - Behavior changes **Exceptions (ask your human partner):** - Throwaway prototypes - Generated code - Configuration files ## The Iron Law ``` NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST ``` Write code before the test? Delete it. Start over. ## Red-Green-Refactor ### RED - Write Failing Test Write one minimal test showing what should happen. ```typescript test('rejects empty email', async () => { const result = await submitForm({ email: '' }); expect(result.error).toBe('Email required'); }); ``` **Requirements:** - One behavior - Clear name - Real code (no mocks unless unavoidable) ### Verify RED - Watch It Fail **MANDATORY. Never skip.** ```bash npm test path/to/test.test.ts ``` Confirm: - Test fails (not errors) - Failure message is expected - Fails because feature missing (not typos) ### GREEN - Minimal Code Write simplest code to pass the test. Don't add features beyond the test. ### Verify GREEN - Watch It Pass **MANDATORY.** Confirm: - Test passes - Other tests still pass - Output pristine (no errors, warnings) ### REFACTOR - Clean Up After green only: - Remove duplication - Improve names - Extract helpers Keep tests green. Don't add behavior. ## Verification Checklist Before marking work complete: - [ ] Every new function/method has a test - [ ] Watched each test fail before implementing - [ ] Each test failed for expected reason - [ ] Wrote minimal code to pass each test - [ ] All tests pass - [ ] Output pristine (no errors, warnings) ## Common Rationalizations | Excuse | Reality | |--------|---------| | "Too simple to test" | Simple code breaks. Test takes 30 seconds. | | "I'll test after" | Tests passing immediately prove nothing. | | "Need to explore first" | Fine. Throw away exploration, start with TDD. | | "TDD will slow me down" | TDD faster than debugging. | ## Red Flags - STOP and Start Over - Code before test - Test after implementation - Test passes immediately - Can't explain why test failed - "Just this once" **All of these mean: Delete code. Start over with TDD.**