Booking backend project · Chapter 11 of 12
Writing tests
By the end of this chapter you can write tests that call Parda's whole API with Vitest and supertest, give every test file a database of its own, test a race between two members and a signed webhook, and tell a test that protects the code from one that only passes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Ready for production
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.
Why you need this
Parda's backend now does everything Diwali week needs: accounts, admin routes, browsing, holds, bookings and payments. Over the next weeks it will keep changing: Asha wants group bookings, Rukhsana wants a member's ticket page, Sayali wants a second payment provider. Every change can break something that works today, and the three things that must never break are the ones that cost money or trust:
- login: a wrong password must never get a session;
- holds: two members must never hold one seat;
- payments: an unsigned webhook must never confirm a booking, and the same event delivered twice must confirm it once.
Until now the Check has tested your code. A Check is not there when Nikhil changes holdSeats on a Friday evening. Tests in the repository are: they run on every change, in seconds, on every developer's machine, and they say what broke before Datta's phone does. In this chapter you write them.
The catch is that a test can pass and protect nothing. A test with no assertion passes. A test that checks the status but not the cookie passes on a login that hands out a cookie scripts can read. So this chapter's Check grades your tests the hard way: it runs them on our code, where they must pass, and on broken copies of our code, where they must fail.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
auth.test.ts: test the login
In tests/auth.test.ts, write tests for POST /auth/login with Neha (people.neha from tests/helpers.ts). With her email and password: assert the status is 200, and that the Set-Cookie header starts with parda_session= and contains HttpOnly. With her email and a wrong password: assert the status is 401 and that there is no Set-Cookie header.
Keep the starter's imports and its line that calls testApp(): the app then runs on a database of this file's own. Run npm test in the terminal.
The Check runs your file three times: on our code, where every test must pass, and on two broken copies of the project, where at least one test must fail: one whose login takes any password, and one whose session cookie is not HttpOnly.
- Task 2
holds.test.ts: test the race for one seat
In tests/holds.test.ts, write a test where Neha and Pranav (people.neha, people.pranav) both hold seat E7 (seeded.e7) of showtime 9 (seeded.showtimeId) at the same moment. Log both in with t.loginAs, send both POST /holds requests together and wait for both, then assert: the two statuses, sorted, are 201 and 409; the 409's body.error.code is seat_taken; and the holds table has exactly one row for that showtime and seat (t.sql runs plain SQL on the test database).
The Check runs your file on our code, where it must pass, and on two broken copies of holdSeats, where it must fail: one that lets both members hold the seat (two 201s), and one that answers the second member with a 500.
- Task 3 · interview question
Interview: payments.test.ts, test the webhook
In tests/payments.test.ts, the starter's beforeAll books E7 and E8 for Neha (booking.id, booking.total) and captured(id) makes a payment.captured event for that total. Write two tests. First: send captured('evt_unsigned') to POST /webhooks/payments with plain supertest and no signature; assert the status is 401, and that the booking's status in the bookings table is still pending. Second: send captured('evt_1') with t.webhook, which signs it; assert the status is 200 and the body has duplicate false and status confirmed; send the same event again; assert the status is 200 and duplicate is true.
The Check runs your file on our code, where it must pass, and on two broken copies, where it must fail: a signature check that lets every body in, and a payment handler that acts on the same event a second time.
Nikhil will ask: a test passes on your code. How do you know it would fail if the code were wrong?
What you will learn
- Vitest: describe, it and expect, and what a failure tells you.
- supertest: calling the Express app with no port, reading the status, the headers and the body; an agent that keeps a cookie.
- A database per test file: why tests never share rows, and what tests/helpers.ts does.
- Testing a race: two requests at once, and an assertion that does not care who wins.
- Testing a webhook: a signed body, an unsigned one, and the same event twice.
- Mutation testing: breaking the code on purpose to see whether a test notices.
More in Ready for production
Build "Writing tests" yourself
Parda sells tickets for Pune's own cinemas and stages. You build its booking API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.