What is TDD?

February 18, 2025

TDD = Test-Driven Development.

The jargon really means one thing, i.e. write tests before writing implementation. It’s about testing the behaviour, not the code itself.

Real Life Example: Testing a ProductCard Component in an E-Commerce App

Let’s say we’re building an e-commerce platform. One of the core UI components is the ProductCard, which displays essential product details like:

To ensure this component behaves as expected, we write a test that ensure the presence of all these elements. The test will pass if all the expected data points are rendered correctly.

What Happens When the UI Changes?

Let's imagine a scenario that often happens in development:

A new developer joins your team and is assigned the task to redesign the storefront. And when updating, the developer mistakenly removes the rating display. And push the changes. Before merging, the CI(Continuous Integration) pipeline will build and test the project, since we wrote a test earlier which expects a rating to be displayed in the ProductCard component, not the test case will fail.

So we have 2 options now:

Why This Matters

Mostly writing test is a checkpoint. Its allow you to decide whether the change was meant to happen or not.

What Kind of Things Should You Test?

AreaExamplesType
Logic/UtilsaddToCart, calculateTotal, filtering, paginationUnit Tests
UI ComponentsProductCard, Cart, Filters, Pagination, FormsComponent Tests
User InteractionsClick “Add to Cart”, filter products, submit formComponent Tests
Core App FlowsBrowse → Add to Cart → Checkout → PaymentE2E Tests
Auth & FormsSignup/Login, contact form validation + submissionE2E + Component

Mindset when building a feature:

  1. Define the expected user behavior.
  2. Write a failing test that reflects that behavior.
  3. Write just enough code to pass the test.
  4. Refactor if required.
  5. Repeat the process.

TLDR;

Get in touch

Do you have an Exciting project for me? Send an E-mail. Or want to have a general conversation? Feel Free to send a message on Twitter.

Drop an e-mail

iamkanhuc@gmail.com

Find me on