What problem does it solves?
This Skill eliminates the risk of writing untested code and ensures your tests genuinely verify behavior. It prevents the common pitfall of writing tests after implementation, which often leads to incomplete or incorrect coverage, saving you debugging time and rework.
Core Features & Use Cases
- RED-GREEN-REFACTOR Cycle: Guides you through the fundamental TDD process: write a failing test, then minimal code to pass it, and finally refactor for cleanliness.
- Behavior-Driven Testing: Forces you to focus on the desired behavior of your code before implementation, ensuring tests are relevant and effective.
- Regression Prevention: Ensures every bug fix or new feature has a test that proves it works and prevents future regressions, building a robust codebase.
- Use Case: When implementing a new user authentication flow, you'd first write a test for a successful login, watch it fail, then write the minimal code to make it pass, ensuring your feature is correctly tested from the start.
Quick Start
Example: Implementing a retry operation
You: I'm using the test-driven-development skill to implement this feature.
RED - Write failing test
test('retries failed operations 3 times', async () => {
let attempts = 0;
const operation = () => {
attempts++;
if (attempts < 3) throw new Error('fail');
return 'success';
};
const result = await retryOperation(operation);
expect(result).toBe('success');
expect(attempts).toBe(3);
});
Verify RED - Watch it fail (MANDATORY)
npm test path/to/test.test.ts # Expected: FAIL (e.g., "retryOperation not defined")
GREEN - Write minimal code
async function retryOperation<T>(fn: () => Promise<T>): Promise<T> {
for (let i = 0; i < 3; i++) {
try { return await fn(); } catch (e) { if (i === 2) throw e; }
}
throw new Error('unreachable');
}
Verify GREEN - Watch it pass (MANDATORY)
npm test path/to/test.test.ts # Expected: PASS
REFACTOR - Clean up (e.g., improve variable names, extract helpers)