This repository contains a series of practice problems designed to help you learn and master Test-Driven Development (TDD) using TypeScript. Each problem is structured to guide you through the TDD cycle: Red (write failing tests), Green (make the tests pass), and Refactor (improve the code while keeping tests green).
- Clone this repository
- Install dependencies:
npm install
- Run tests:
npm test
- For watch mode (recommended for TDD):
npm run test:watch
The project contains three practice problems, each in its own directory:
src/
├── string-calculator/ # Problem 1: String Calculator
├── fizz-buzz/ # Problem 2: FizzBuzz
└── palindrome/ # Problem 3: Palindrome Checker
Each problem directory contains:
- An implementation file (
.ts
) - A test file (
.test.ts
) - TODO comments describing the requirements
- Empty test cases for you to implement
A classic TDD kata that helps you learn the fundamentals of TDD. You'll implement a calculator that takes a string of numbers and returns their sum, with various rules for handling different formats and edge cases.
Learning Objectives:
- Writing your first tests
- Handling edge cases
- String parsing
- Error handling
- Incremental development
A well-known programming problem that's perfect for practicing TDD. You'll implement a program that converts numbers to strings based on divisibility rules, and generate sequences of these conversions.
Learning Objectives:
- Writing clear, focused tests
- Handling multiple conditions
- Working with sequences
- Refactoring for clarity
A more complex problem that involves string manipulation and multiple methods. You'll implement different ways to check for palindromes, from simple word checks to complex sentence analysis.
Learning Objectives:
- Testing multiple methods
- String manipulation
- Edge case handling
- Working with different requirements
-
Read the Requirements
- Start by reading the TODO comments in both the implementation and test files
- Understand what each method should do
- Note the edge cases and special conditions
-
Write Your First Test
- Choose the simplest requirement to implement first
- Write a test that describes the expected behavior
- Run the test to see it fail (Red)
-
Make the Test Pass
- Implement the minimum code necessary to make the test pass
- Don't worry about other requirements yet
- Run the test to see it pass (Green)
-
Refactor
- Look for ways to improve your code
- Make sure all tests still pass
- Repeat the cycle for the next requirement
-
Write Tests First
- Always write the test before implementing the feature
- Let the test guide your implementation
- Make the test fail first to ensure it's working
-
Keep Tests Simple
- One assertion per test when possible
- Clear test names that describe the behavior
- Focus on one requirement at a time
-
Incremental Development
- Add one feature at a time
- Make small, focused changes
- Keep the code working at all times
-
Refactor Regularly
- Look for code smells
- Improve readability
- Remove duplication
- Keep the code clean
-
Start Small
- Begin with the simplest test case
- Build up complexity gradually
- Don't try to implement everything at once
-
Read the Tests
- The test file contains hints about the implementation
- Use the test descriptions to guide your code
- Make sure your implementation matches the test expectations
-
Use Watch Mode
- Run tests in watch mode to get immediate feedback
- See test results as you type
- Catch issues early
-
Commit Often
- Make small, focused commits
- Document your progress
- Keep a history of your TDD journey
If you get stuck:
- Read the test requirements again
- Look at the examples in the comments
- Try writing a simpler test first
- Break down the problem into smaller steps
Remember: The goal is to learn TDD, not just to solve the problems. Take your time, write good tests, and enjoy the process!
This project is licensed under the ISC License.