Test-Driven Development (TDD)
Testing Strategy
Test Types
- Unit Tests: Business logic, utilities, helpers.
- Component Tests: UI component behavior (rendering, props).
- Integration Tests: Feature flows and service interactions.
- E2E Tests: Critical user journeys (login, checkout, etc.).
Testing Tools
- Unit/Component: Jest, React Testing Library (RTL), Flutter Test.
- E2E: Detox (React Native), Appium, or Flutter Integration Test.
- Coverage: Aim for 80% minimum code coverage.
Test Structure
- Naming: Use descriptive test names that explain the scenario and expected result.
- AAA Pattern: Follow Arrange (setup), Act (execute), Assert (verify).
- Mocking: Mock external dependencies (APIs, DBs) to isolate the unit under test.
- Scenarios: Test happy paths, edge cases, and error scenarios.
What to Test
- Rendering: Components render correctly with different props/states.
- Interactions: Tap, swipe, text input, form submission.
- Integration: API calls (mocked) and data parsing.
- Navigation: Correct screens are pushed/popped.
- Logic: State management reducers/providers and form validation.
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.
Violating the letter of the rules is violating the spirit of the rules.
When to Use
Always:
- New features
- Bug fixes
- Refactoring
- Behavior changes
Exceptions:
- 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 one minimal test showing what should happen. Run it. Confirm it fails for the right reason.
- GREEN: Write simplest code to pass the test. Run it. Confirm it passes.
- REFACTOR: Clean up code. Confirm tests still pass.
Good Tests
- Minimal: One thing per test.
- Clear: Name describes behavior.
- Shows intent: Demonstrates desired API.
Why Order Matters
Tests written after implementation:
- Might test wrong thing
- Might test implementation, not behavior
- Might miss edge cases you forgot
- You never saw it catch the bug
Test-first forces you to see the test fail, proving it actually tests something.
Common Rationalizations (Ignore These)
- "Too simple to test" -> Simple code breaks.
- "I'll test after" -> Proof of nothing.
- "I already manually tested" -> Ad-hoc, not repeatable.
- "Deleting implementation is wasteful" -> Keeping unverified code is technical debt.
Red Flags - STOP and Start Over
- Code before test
- Test after implementation
- Test passes immediately
- Can't explain why test failed
- "I already manually tested it"
All of these mean: Delete code. Start over with TDD.
Verification Checklist
Before marking work complete:
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: test-driven-development-53description: Use when implementing any feature or bugfix, before writing implementation code4---56# Test-Driven Development (TDD)78## Testing Strategy910### Test Types111. **Unit Tests**: Business logic, utilities, helpers.122. **Component Tests**: UI component behavior (rendering, props).133. **Integration Tests**: Feature flows and service interactions.144. **E2E Tests**: Critical user journeys (login, checkout, etc.).1516### Testing Tools17- **Unit/Component**: Jest, React Testing Library (RTL), Flutter Test.18- **E2E**: Detox (React Native), Appium, or Flutter Integration Test.19- **Coverage**: Aim for **80% minimum** code coverage.2021### Test Structure22- **Naming**: Use descriptive test names that explain the *scenario* and *expected result*.23- **AAA Pattern**: Follow **Arrange** (setup), **Act** (execute), **Assert** (verify).24- **Mocking**: Mock external dependencies (APIs, DBs) to isolate the unit under test.25- **Scenarios**: Test happy paths, edge cases, and error scenarios.2627### What to Test28- **Rendering**: Components render correctly with different props/states.29- **Interactions**: Tap, swipe, text input, form submission.30- **Integration**: API calls (mocked) and data parsing.31- **Navigation**: Correct screens are pushed/popped.32- **Logic**: State management reducers/providers and form validation.3334## Overview3536Write the test first. Watch it fail. Write minimal code to pass.3738**Core principle:** If you didn't watch the test fail, you don't know if it tests the right thing.3940**Violating the letter of the rules is violating the spirit of the rules.**4142## When to Use4344**Always:**45- New features46- Bug fixes47- Refactoring48- Behavior changes4950**Exceptions:**51- Throwaway prototypes52- Generated code53- Configuration files5455## The Iron Law5657```58NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST59```6061Write code before the test? Delete it. Start over.6263## Red-Green-Refactor64651. **RED:** Write one minimal test showing what should happen. Run it. Confirm it fails for the right reason.662. **GREEN:** Write simplest code to pass the test. Run it. Confirm it passes.673. **REFACTOR:** Clean up code. Confirm tests still pass.6869## Good Tests7071- **Minimal:** One thing per test.72- **Clear:** Name describes behavior.73- **Shows intent:** Demonstrates desired API.7475## Why Order Matters7677Tests written after implementation:78- Might test wrong thing79- Might test implementation, not behavior80- Might miss edge cases you forgot81- You never saw it catch the bug8283**Test-first forces you to see the test fail, proving it actually tests something.**8485## Common Rationalizations (Ignore These)8687- "Too simple to test" -> Simple code breaks.88- "I'll test after" -> Proof of nothing.89- "I already manually tested" -> Ad-hoc, not repeatable.90- "Deleting implementation is wasteful" -> Keeping unverified code is technical debt.9192## Red Flags - STOP and Start Over9394- Code before test95- Test after implementation96- Test passes immediately97- Can't explain why test failed98- "I already manually tested it"99100**All of these mean: Delete code. Start over with TDD.**101102## Verification Checklist103104Before marking work complete:105106- [ ] Every new function/method has a test107- [ ] Watched each test fail before implementing108- [ ] Each test failed for expected reason109- [ ] Wrote minimal code to pass each test110- [ ] All tests pass111- [ ] Output pristine (no errors, warnings)112113---114> Converted and distributed by [TomeVault](https://tomevault.io/claim/mkas08) — claim your Tome and manage your conversions.115<!-- tomevault:4.0:skill_md:2026-04-16 -->