Search⌘ K

Test-Driven Development

Explore the principles of test-driven development (TDD) within a TypeScript functional programming context. Learn to write tests before code, build functions iteratively, and ensure high test coverage. Understand TDD's benefits for code quality, refactoring, and thoughtful functionality design.

Perhaps a brief explanation of that concept is in order. Test-driven development (TDD) comes from the Extreme Programming movement. Its core idea is to create a test that verifies the code’s behavior before actually writing that code. Let’s write a function that adds two numbers.

Writing test

Before writing code, we’ll create the following test using the Jest framework:

TypeScript 3.3.4
describe('sum tests', () => {
test('should return two when given one and one', () => {
const result = sum(1, 1);
expect(result).toBe(2);
});
});

Obviously, this test fails because we lack a function that adds numbers. We don’t even have a sum yet. Our first step is to compile our ...