TDD Practice: Build a String Formatter
What you'll build
A string formatter utility with methods for camelCase, kebab-case, snake_case, and title case conversion, built entirely using the TDD Red-Green-Refactor cycle.
Goals
- Practice the TDD Red-Green-Refactor cycle
- Write failing tests before implementation
- Write minimal code to pass tests
- Refactor with confidence (tests protect you)
- Build 4 string formatting functions
Prerequisites
- Completed Project 1 or equivalent Jest experience
- Understanding of string methods in JavaScript
- Familiarity with the TDD concept
Steps
- 1
RED: Write the first failing test
Create
stringFormatter.test.js. Write a test for camelCase conversion before any implementation exists. Run it — it should fail (Red phase).Codeconst { toCamelCase } = require('./stringFormatter'); test('converts space-separated to camelCase', () => { expect(toCamelCase('hello world')).toBe('helloWorld'); });Expected result
Test fails with 'Cannot find module' or 'toCamelCase is not a function'.
- 2
GREEN: Write minimal code to pass
Create
stringFormatter.jswith the simplest implementation that passes the test.Codefunction toCamelCase(str) { return str.split(' ').map((word, i) => i === 0 ? word : word[0].toUpperCase() + word.slice(1) ).join(''); } module.exports = { toCamelCase };Expected result
Test passes (Green phase).
- 3
RED→GREEN: Add kebab-case
Write a failing test for toKebabCase, then implement it. Follow the cycle: write test (red), implement (green).
Code// Test (RED) test('converts to kebab-case', () => { expect(toKebabCase('Hello World')).toBe('hello-world'); }); // Implementation (GREEN) function toKebabCase(str) { return str.toLowerCase().replace(/\s+/g, '-'); }Expected result
New test passes after implementation.
- 4
REFACTOR: Improve the code
Now that tests pass, refactor the code for clarity. Extract shared logic, improve variable names. Run tests after each change to ensure nothing breaks.
Expected result
Code is cleaner, all tests still pass.
Hint
Refactoring is about improving structure without changing behavior.
- 5
Continue TDD for snake_case and title case
Repeat the Red-Green-Refactor cycle for toSnakeCase and toTitleCase. Write the test first, watch it fail, implement, watch it pass, then refactor.
Expected result
All four functions are implemented with full test coverage.
Try it yourself
What you learned
You experienced the TDD workflow firsthand: writing tests first forces you to think about the interface before implementation. The Red-Green-Refactor cycle gives you confidence — tests protect you during refactoring and document the expected behavior.
Related Resources
Related Lessons
- Test-Driven Development
The TDD workflow.
Related Practice
- TDD Exercises
Practice TDD concepts.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.