awesome-everything RU
↑ Back to the climb

Engineering Practice

TDD: code and test reading

Crux Read real test code — a first failing test, a fragile mock-heavy test, a property, and a surviving mutant — and pick the fix or diagnosis a senior makes first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Test quality is judged in the test file, not the coverage report. Read each snippet the way you’d read a colleague’s PR, then choose the change a senior would make first.

Goal

Practise the eye you bring to a real review: spot the test that asserts structure instead of behavior, write the failing test that drives design, state a property that beats examples, and read what a surviving mutant is telling you.

Snippet 1 — the first failing test

// Driving a not-yet-built refund rule, test-first.
test("refund for a fully-shipped order is rejected", () => {
  const order = anOrder({ status: "SHIPPED" });
  const result = refundPolicy.evaluate(order);
  expect(result.allowed).toBe(false);
  expect(result.reason).toBe("ALREADY_SHIPPED");
});
Quiz

Before refundPolicy exists, what does writing this test first buy you, and why is it shaped well?

Snippet 2 — the fragile test

test("checkout charges the customer", () => {
  const tax = mock(TaxRule);          // pure, owned by us
  const gateway = mock(PaymentGateway); // third-party network
  when(tax.apply(100)).thenReturn(108);
  checkout.pay(cart, tax, gateway);
  verify(tax.apply(100)).once();      // assert the call happened
  verify(gateway.charge(108)).once();
});
Quiz

A behavior-preserving refactor of checkout turns this test red, yet a real total-computation bug once shipped with it green. What's the root cause?

Snippet 3 — the property

// fast-check: rewriting a battle-tested CSV parser, oldParse is the trusted reference.
fc.assert(fc.property(fc.string(), (raw) => {
  expect(newParse(raw)).toEqual(oldParse(raw)); // oracle property
}), { numRuns: 1000 });
Quiz

Which statement about this property is correct?

Snippet 4 — the surviving mutant

// Production code under mutation testing:
function withinBudget(amount, limit) {
  return amount <= limit;   // Stryker mutated this to: amount < limit
}
// The full suite still passed after the mutation. Mutant SURVIVED.
Quiz

The '<=' → '<' mutant survived with 100% line coverage. What does that tell you, and what's the fix?

Recap

Every test in this unit is read the same way. The first failing test is design feedback — it fixes the interface and asserts an observable outcome, so it survives refactors. The fragile test over-mocks owned, pure code: it breaks on refactor and ships bugs green behind stubbed results — use real objects and assert state. The oracle property lets a trusted reference define correctness over generated input and shrinks to a minimal counterexample. And a surviving mutant is a named missing assertion — most often a boundary the suite never fed. Diagnose from the test, fix the assertion, re-run to confirm the mutant is killed.

Continue the climb ↑TDD: build a suite that would notice the bug
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.