In this chapter
We'll learn why developers write code that checks their code — test cases and assertions, and the three main kinds of tests: unit, integration and end-to-end — explained as checking a car part by part, together, and on a real drive.
The Problem in Real Life
"But I tested it," Anna says. "I tried Berlin, New York, London, Tokyo. It works." John nods. "You tested it today, by hand. In six months, someone will change formatShowTime to add a new feature. Will they try four timezones by hand? Will they even know they should?"
"An automated test is you, checking those four timezones, every time anyone changes the code — forever — in two seconds. That's how a fixed bug stays fixed."
A fixed bug without a test is a bug on holiday.
John
Checking by Hand Once vs. Checking Automatically Forever
Bugs come back
A later change can quietly bring an old bug back. Nobody remembers to check by hand.
Manual checks are slow
Clicking through every page after every change takes hours. Tests do it in seconds.
Confidence to change code
Without tests, every change feels risky. With them, you know right away if you broke something.
Test Cases, Assertions and the Three Kinds of Tests
The car factory analogy: a car factory doesn't build a whole car and then hope it drives. It checks each part on its own (does this brake pad grip?), then checks parts working together (does the brake pedal actually stop the wheel?), and finally does a real test drive on a track. Software testing has the same three levels.
- Test — code that checks code: an automated test runs a piece of your code with a known input and checks that the output is what you expect. If not, the test fails and tells you exactly what was expected and what came out. Tests live in the project (Act 16's
tests/folder) and run with one command, likenpm test. - Test case — one situation to check: a test case is one specific scenario: "a show at 20:00 UTC at a London venue in November shows 8:00 PM." Good test cases cover the normal case, the edge cases (Act 08's "what ifs": midnight, clock-change day) and the bug that just happened — that one's called a regression test, because it stops the bug from coming back (regressing).
- Assertion — the actual check: an assertion is the line that says what must be true:
expect(result).toBe("8:00 PM"). Read it as a sentence: "I expect the result to be 8:00 PM." A test can have several assertions; if any one fails, the test fails. - Unit test — checking one part on the bench: tests one small piece — usually one function — on its own, with no database, no network. Very fast (milliseconds) and precise: when it fails, you know exactly where. Most of a project's tests are unit tests.
- Integration test — parts working together: tests that several pieces work together correctly: the API route, the database and the formatting code. For example: "
GET /api/events/riverside-arena-1114returnsdisplayTime8:00 PM". Slower, because it uses a real (test) database, but it catches problems that unit tests can't — like the API forgetting to pass the venue's timezone. - End-to-end (E2E) test — the real test drive: a robot uses the app like a real person: opens a real browser, visits the page, clicks, reads what's on the screen. Tools: Playwright, Cypress. Slowest and most fragile, but closest to what users experience. Keep them for the most important journeys — like buying a ticket.
| Kind | Car factory version | BlueTicket example | Speed |
|---|---|---|---|
| Unit | Check one brake pad | formatShowTime returns 8:00 PM for London | Milliseconds |
| Integration | Pedal + brake + wheel together | The event API returns displayTime 8:00 PM | Seconds |
| End-to-end | A real test drive | A browser opens the event page and sees 8:00 PM | Many seconds |
| Test case | Input | Expected |
|---|---|---|
| London venue, November | 2026-11-14T20:00:00Z, Europe/London | 8:00 PM |
| Berlin venue, November | 2026-11-14T19:00:00Z, Europe/Berlin | 8:00 PM |
| New York venue | 2026-11-15T01:00:00Z, America/New_York | 8:00 PM |
| London, clock-change day | 2026-10-25T19:00:00Z, Europe/London | 7:00 PM |
| Regression: server in Berlin | Tests run with TZ=Europe/Berlin | All of the above still pass |
The testing pyramid
End-to-end tests (a few)
real browser, real journey — slowest
Integration tests (some)
API + database + code together
Unit tests (many)
one function at a time — milliseconds
import { describe, it, expect } from "vitest";import { formatShowTime } from "../src/events/format";describe("formatShowTime", () => {it("converts show time to venue timezone (London)", () => {const result = formatShowTime(new Date("2026-11-14T20:00:00Z"), "Europe/London");expect(result).toBe("8:00 PM");});it("converts show time to venue timezone (New York)", () => {const result = formatShowTime(new Date("2026-11-15T01:00:00Z"), "America/New_York");expect(result).toBe("8:00 PM");});it("handles the day the clocks change", () => {const result = formatShowTime(new Date("2026-10-25T19:00:00Z"), "Europe/London");expect(result).toBe("7:00 PM");});});
Each it(...) is one test case; each expect(...).toBe(...) is an assertion. The test names read like sentences.
TZ=Europe/Berlin npm test✓ formatShowTime > converts show time to venue timezone (London)✓ formatShowTime > converts show time to venue timezone (New York)✓ formatShowTime > handles the day the clocks change✓ GET /api/events/:id > returns displayTime in the venue's timezoneTests 4 passed (4)
The testing pyramid: many fast unit tests at the bottom, fewer integration tests in the middle, a handful of E2E tests at the top. Like the car factory: thousands of part checks, fewer assembly checks, a few test drives.
Why tests matter: they catch bugs before users do; they stop old bugs from returning; they let you change code with confidence; and they act as documentation — a test called "formats show time in the venue's timezone" tells the next developer exactly what the code must do. In Act 19, BlueTicket's pipeline will run every test automatically on every pull request and refuse to deploy if any fail.
Anna's tests: she writes four unit test cases for formatShowTime — London, Berlin, New York, and the October clock-change day — and an integration test for the event API. Then she does something clever: she sets the test run itself to TZ=Europe/Berlin, so the tests run in the exact situation that caused the bug. She temporarily puts back the old getHours() code — the tests go red. She restores the fix — green. A test you've never seen fail might not be testing anything. John approves the pull request.
Key Takeaway
A test is code that checks code: a test case is one situation, an assertion is the line saying what must be true. Like a car factory, check parts alone (unit tests: fast, precise), parts together (integration tests: API + database) and real journeys (end-to-end tests in a real browser). Keep many unit tests, fewer integration tests and a few E2E tests. Every fixed bug gets a regression test — and watch it fail once to know it really works.
Why This Matters
Tests are how professional teams change code every day without breaking what already works. Most companies expect every change — and every bug fix — to come with tests, and code reviewers will ask for them. Learning to write a clear test case with a good assertion is one of the most useful habits you can build in your first year.
The fix is deployed. The Riverside Arena's box office replies: "8:00 PM. Perfect, thank you." The bug is fixed — and, thanks to the tests, it's not just on holiday. John has one more exercise for Anna: a brand-new bug report, from start to finish.
