17 min read
Unit, integration and end-to-end tests
In one sentence: A unit test tests a piece on its own; an integration test makes it work with the real thing, like PostgreSQL; and an end-to-end test uses the whole application from the outside, over HTTP, like a customer. The more a test covers, the more bugs it catches and the more it costs. The shop bets on integration tests because its bugs live in the database.
What you will learn
- Tell a unit test, an integration test and an end-to-end test apart, and classify the shop’s.
- Choose the type of test that pays off in each case, by what it costs and what it catches.
- Run an end-to-end test against the Compose shop and read a coverage report without chasing 100%.
- Recognize a flaky test and look for its cause instead of rerunning it until it passes.
Before you start, read: 01-writing-tests.md phase-7/07-compose.md phase-9/02-cache.md
The problem
Section titled “The problem”Imagine you edit Mailpit’s settings in compose.yaml and, by accident, change the port it receives email on. We tried it: npm test comes out with 325 green tests, the shop starts healthy and answers everything, and no email arrives. The worker tries five times, writes ECONNREFUSED in its log and gives up.
No test noticed because none of the 325 starts Docker. Almost all of them call the shop inside their own Node process. Whether the pieces fit together in Compose, with their worker and their Mailpit, nobody checks.
Each type of test catches different bugs at a different price. Here you classify the shop’s tests, run the type it was missing, read which lines nobody runs and meet the test that sometimes fails without anyone touching the code.
The analogy
Section titled “The analogy”The analogy
A theater company is putting on a play. Each actor goes over their lines at home, in ten minutes. Then they rehearse scene by scene, and that’s where you see whether the lines fit together. Before opening night, the dress rehearsal: costumes, lights and the whole play.
The dress rehearsal is the one most like the real show, and the most expensive to set up. If something goes wrong, you don’t know right away whether it was the lights, the curtain or the actor. Each kind of rehearsal catches what the others don’t see.
Where the analogy breaks down
- The dress rehearsal happens once. The shop runs its start-to-finish test before every deployment, and a machine runs it.
- Every night, the actors do it a little differently. Your code, with the same things around it, does the same thing every time: if a test sometimes passes and sometimes doesn’t, something around it has changed, like time, load or order.
How it really works
Section titled “How it really works”Three types of test, by how far they reach
Section titled “Three types of test, by how far they reach”They differ in how much of the application each test starts. The shop has one of each:
- A Unit testAn automatic test that checks one small piece of code, usually a function, with no database and no network. You give it some data, look at what comes back and check it is what you expected. It runs in milliseconds.Go to definition tests a piece on its own, with no database or network: the five in
test/signature.test.ts, from lesson 1, callvalidSignatureand look at what it returns. - An Integration testAn automatic test that makes several real pieces work together, for example your code and a real PostgreSQL database, to check that they fit. It takes longer than a unit test, but it finds failures that a unit test cannot see.Go to definition makes several real pieces work together: the ones in
test/transactions.test.tssend requests withapp.request, with no network, and the shop handles them with a real PostgreSQL,shop_test. - An End-to-end testAn automatic test that uses the whole program from the outside, the way a client would: it sends a real HTTP request and looks at the response. It is the closest to real use and also the slowest.Go to definition uses the whole application from the outside, like a customer: the Compose shop, over HTTP. The first one arrives in this step:
test/e2e/purchase.e2e.ts.
What the shop has: almost all integration
Section titled “What the shop has: almost all integration”In the 39 files in test/ of step 42:
- 31 talk to a real PostgreSQL: 28 start with
useDb()(lesson 1) and another 3 use it directly. - 27 call the shop with
app.request, inside the same process. A few start the server separately, because what they test, like shutting the shop down, can’t be seen any other way. - 4 check configuration files, like
compose.yamlor theDockerfile, almost always by reading their lines. - There are few unit tests: the five for the signature and an occasional standalone test, like the one that checks that the cache’s TTL varies a little.
The shop is, above all, a shop of integration tests.
What each type costs
Section titled “What each type costs”Each type pays for its reach. The times are from our machine:
| Unit | Integration | End-to-end | |
|---|---|---|---|
| In the shop | signature.test.ts |
transactions.test.ts |
purchase.e2e.ts |
| What it needs | Node | Node and PostgreSQL with shop_test |
The whole shop in Compose |
| How long one takes | Under 2 ms | From 1 to about 120 ms, and one over half a second | 1.4 s the purchase, with the wait for the email |
| When it fails, it tells you | The exact function and case | The route and what it left in the database | That something in the chain failed, not what |
| What breaks it without the code being wrong | Almost nothing | A stopped PostgreSQL | A stopped shop, another port, an email that takes a while |
The more a test covers, the more things outside your code can turn it red.
The pyramid and the trophy
Section titled “The pyramid and the trophy”There are two famous ways of saying how many tests of each type you should have. The pyramid, by Mike Cohn, puts lots of unit tests at the base, because they’re cheap. The trophy, by Kent C. Dodds, puts static checks at the base (in the shop, npm run check, from Phase 3) and leaves the widest part for integration:
| Type | Pyramid | Trophy | The shop |
|---|---|---|---|
| Static (types) | Doesn’t count them | The base | npm run check, in every step |
| Unit | Most | A few | Few |
| Integration | Some | Most | Almost all |
| End-to-end | Very few | Few | 2, since step 42 |
They agree on few end-to-end tests; they disagree in the middle.
Why the shop looks like the trophy
Section titled “Why the shop looks like the trophy”A test is useful where the bug it has to catch lives. The shop’s live in the database: the quantity one (introduction) was in which UPDATE was run and in which transaction, and a fake PostgreSQL would never have caught it (lesson 1).
Almost all of the shop’s code receives a request, talks to PostgreSQL and answers: it has little logic that works on its own. A program that calculates taxes or discounts, with lots of rules and no database, would have many more unit tests.
The shop uses the PostgreSQL on your computer and, in GitHub ActionsThe GitHub service that runs tasks when something happens in a repository, such as a push or a pull request, for example running the tests or publishing an image. They are described in YAML files in .github/workflows, and every job runs on a fresh virtual machine GitHub lends.Go to definition, one in a container (Phase 8). Testcontainers, another widely used way, starts it from the tests’ own code.
End-to-end: the whole shop, over HTTP only
Section titled “End-to-end: the whole shop, over HTTP only”test/e2e/purchase.e2e.ts imports nothing from src/: it talks to the shop with fetch, like any customer. Its first test makes a whole purchase:
- It signs in as Carmen, one of the practice accounts in the development data, and keeps the .
- It places an order and pays for it with the fake gateway,
npm run gateway(Phase 4). - It waits for the confirmation email in Mailpit, asking every second, for 30 at most, and keeps the one for its order by the subject: “Your order … is paid.”
- It cancels the order, to give what it bought back to the stock: otherwise, a reader who repeats the test would slowly empty the shop, and around the 40th run the order would be a
409.
It doesn’t create accounts on purpose: the shop allows creating 5 per hour from the same IP (Phase 6), and the sixth run would fail. An end-to-end test runs into all of the real shop’s rules.
The second one checks /health and the 404 of a route that doesn’t exist.
The purchase goes through the Docker image, the API, PostgreSQL, the queue, the worker and Mailpit. If this test fails and the others don’t, the bug is in how the pieces fit together.
They end in .e2e.ts, so npm test, which only takes test/*.test.ts, doesn’t run them: without the shop running they’d always fail. You run them with npm run e2e, which looks for the shop at BASE_URL (by default, http://127.0.0.1:3000) and for Mailpit at MAILPIT_URL (http://127.0.0.1:8025).
Coverage: the lines no test runs
Section titled “Coverage: the lines no test runs”Code coverageThe percentage of your code’s lines that run when the tests run. A line with no coverage has not been tested by anyone; a covered line is not necessarily well tested, because running it is not the same as checking it.Go to definition counts which lines of your code run while the tests pass. Node measures it with the --experimental-test-coverage option, and step 42 brings a script with it, npm run coverage: it runs the tests of npm test and ends with a table, one row per file:
line %: the percentage of its lines that some test has run.branch %: that of its branches. Anifhas two paths, the yes one and the no one.funcs %: that of its functions someone has called.uncovered lines: the lines nobody has run. It’s the useful column.
The files in test/ don’t show up (--test-coverage-exclude='test/**'): what matters is how much of the shop they run.
Why 100% isn’t the goal
Section titled “Why 100% isn’t the goal”A high percentage says less than it seems:
- Running isn’t checking. A test with no assertions that calls
validSignaturealso covers its lines. - What’s missing doesn’t show up. The quantity bug was an
UPDATEthat didn’t exist, and a line that doesn’t exist can’t show up as “not run.” - The last lines are the most expensive. They tend to be the ones for rare errors, like a dropped connection, and causing them in a test costs more than all the rest.
Coverage is a map of where nobody has looked, not a grade. You read the uncovered lines, one by one, and decide which ones deserve a test.
Flaky tests: green, red, green, without touching anything
Section titled “Flaky tests: green, red, green, without touching anything”A Flaky testA test that sometimes passes and sometimes fails although the code has not changed, almost always because it depends on the clock, the order of execution or something external. It is dangerous because it teaches people to ignore failures.Go to definition sometimes passes and sometimes fails, with the same code. The shop had one.
On October 22, 2026, while Phase 9 was being prepared, GitHub Actions ran the cache tests in their eight copies (steps 38 to 41, in both languages). In seven, “buying checks the stock in the database even if the cache says otherwise” passed in about 12 milliseconds. In the English copy of step 38, it failed: two seconds later, product 4’s key still wasn’t in the cache.
If a command to the cache takes longer than 200 milliseconds, the shop takes it as down and reads from PostgreSQL without storing anything. The explanation the fix gave, the most likely one: GitHub’s machine, under load, took too long once. The test read the product only once and expected a key that read wasn’t going to store.
The shop did the right thing; the test assumed something false: that a read always stores. The fix, in test/cache.test.ts, is readUntilKeysAre: it repeats the reads until the keys appear, for 5 seconds at most.
What a flaky test depends on
Section titled “What a flaky test depends on”Almost always, on something the test doesn’t control:
- Time. Sleeping a fixed two seconds and assuming it’s enough. Better to wait for a condition with a maximum, like the end-to-end test with the email, or the fake clock from lesson 1.
- What another test leaves behind. If they share data, the result depends on the order: Carmen’s inbox keeps the emails of other runs, and that’s why the end-to-end test looks for the one for its order.
- A real race. In lesson 1, “paying and changing the quantity at the same time,” with the stock broken, failed or not depending on who got there first: that occasional red was a real bug.
What you don’t do is run it again until it comes out green. A test that sometimes fails teaches the team not to trust red, and the day the red is real, nobody looks at it.
Where each test runs
Section titled “Where each test runs”npm test, with every change, and in your workflow (Phase 8) before publishing the image.npm run e2e, before deploying, with the Compose shop running. Your workflow doesn’t run it: it would have to bring up the whole of Compose.npm run coverage, now and then, to look for gaps: it isn’t a gate for deploying.
Try it
Section titled “Try it”The goal: check that the Compose shop sells from start to finish and find a line no test runs, to decide whether it deserves one.
The outputs are from Node 26, on Linux, on October 24, 2026; your times will be different. You use step 42-tests from lesson 1.
Start the shop
Section titled “Start the shop”From your project, with your usual .env:
cd ~/backend-course/phase-3/shop-apidocker compose up -d --build --waitdocker compose port api 3000--wait returns when everything is healthy. The last command says the API’s port: if it’s 3001, type BASE_URL=http://127.0.0.1:3001 npm run e2e instead of npm run e2e.
A purchase from start to finish
Section titled “A purchase from start to finish”1. The end-to-end tests
npm run e2eWhat you will see:
> shop-api@1.0.0 e2e> node --env-file-if-exists=.env --test --test-concurrency=1 'test/e2e/*.e2e.ts'
✔ a purchase from start to finish: session, order, payment and email (1400.160664ms)✔ the shop answers on /health, and what does not exist is a 404 in Problem Details format (6.49227ms)ℹ tests 2ℹ suites 0ℹ pass 2ℹ fail 0ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 1474.560372The first two lines are npm’s: the command behind the script. Look at pass 2: Carmen has bought, paid and received her email. The purchase takes 1.4 seconds, with the wait for the email: the test asks Mailpit every second, until it arrives. We ran it seven times in a row: all seven green. Once, the purchase took a quarter of a second: the email was already there the first time the test asked.
If it fails:
- Both, with
fetch failedandECONNREFUSED: nobody is listening at that address. The shop isn’t running, or it’s on3001. - The first one, because the email didn’t arrive: check whether the worker is running, with
docker compose ps.
Challenge: stop only the API, with docker compose stop api, and run npm run e2e again. How many fail, and what does the error say? Has anything changed in the code?Show answer
Both fail, in milliseconds, with fetch failed and ECONNREFUSED, and the code is the same as a minute ago. An end-to-end test turns red for things that aren’t your code: that’s part of its price. npm test would stay green. Start it again with docker compose start api, and check its port with docker compose port api 3000: ours came back on 3001.
The lines no test runs
Section titled “The lines no test runs”You need the same as for npm test: PostgreSQL with shop_test and the tests’ Redis (“Before you start”).
2. The coverage report
npm run coverageWhat you will see:
ℹ tests 325ℹ suites 0ℹ pass 325ℹ fail 0ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 146993.660283ℹ start of coverage reportℹ ----------------------------------------------------------------------ℹ file | line % | branch % | funcs % | uncovered linesℹ ----------------------------------------------------------------------ℹ drizzle.config.ts | 100.00 | 87.50 | 100.00 |ℹ examples | | | |ℹ fast-hash.ts | 100.00 | 100.00 | 100.00 |ℹ injection.ts | 100.00 | 100.00 | 100.00 |ℹ jwt.ts | 100.00 | 91.67 | 100.00 |ℹ money.ts | 100.00 | 100.00 | 100.00 |ℹ n-plus-one.ts | 100.00 | 100.00 | 100.00 |ℹ protobuf.ts | 100.00 | 100.00 | 100.00 |ℹ race.ts | 100.00 | 87.50 | 100.00 |ℹ unsafe-page.ts | 100.00 | 100.00 | 100.00 |ℹ scripts | | | |ℹ create-admin.ts | 100.00 | 100.00 | 100.00 |ℹ create-key.ts | 84.21 | 50.00 | 100.00 | 8-10ℹ many-orders.ts | 100.00 | 100.00 | 100.00 |ℹ migrate.ts | 100.00 | 100.00 | 100.00 |ℹ other-site.ts | 88.33 | 66.67 | 33.33 | 54-60ℹ payment-gateway.ts | 93.62 | 50.00 | 100.00 | 18-20ℹ provider.ts | 86.89 | 75.00 | 100.00 | 46 55-61ℹ seed.ts | 100.00 | 100.00 | 100.00 |ℹ worker.ts | 90.00 | 66.67 | 66.67 | 16-18 55-57ℹ src | | | |ℹ api-keys.ts | 100.00 | 100.00 | 100.00 |ℹ app.ts | 100.00 | 100.00 | 100.00 |ℹ cache.ts | 99.15 | 96.61 | 93.75 | 160-161ℹ config.ts | 100.00 | 100.00 | 100.00 |ℹ contract.ts | 100.00 | 100.00 | 100.00 |ℹ data.ts | 100.00 | 100.00 | 100.00 |ℹ db.ts | 97.44 | 100.00 | 0.00 | 22ℹ echo.ts | 100.00 | 87.50 | 85.71 |ℹ email.ts | 100.00 | 83.33 | 100.00 |ℹ heartbeat.ts | 87.18 | 85.71 | 100.00 | 28-32ℹ log.ts | 100.00 | 100.00 | 100.00 |ℹ middleware | | | |ℹ api-key.ts | 100.00 | 100.00 | 100.00 |ℹ idempotency.ts | 100.00 | 92.86 | 100.00 |ℹ request-log.ts | 100.00 | 100.00 | 100.00 |ℹ session.ts | 100.00 | 100.00 | 100.00 |ℹ validate.ts | 100.00 | 100.00 | 100.00 |ℹ verify-signature.ts | 100.00 | 100.00 | 100.00 |ℹ notifications.ts | 100.00 | 100.00 | 100.00 |ℹ order-events.ts | 98.38 | 95.12 | 95.65 | 182-185ℹ outbox.ts | 100.00 | 96.15 | 100.00 |ℹ pagination.ts | 100.00 | 88.89 | 100.00 |ℹ passwords.ts | 100.00 | 100.00 | 100.00 |ℹ permissions.ts | 100.00 | 100.00 | 100.00 |ℹ problems.ts | 100.00 | 80.00 | 100.00 |ℹ provider.ts | 98.61 | 58.82 | 87.50 | 41-42ℹ queue.ts | 100.00 | 80.95 | 78.57 |ℹ rate-limits.ts | 100.00 | 97.06 | 100.00 |ℹ redis-store.ts | 100.00 | 100.00 | 85.71 |ℹ safe-url.ts | 96.64 | 94.59 | 75.00 | 135-139ℹ schema.ts | 95.22 | 95.74 | 84.00 | 182-185 187-192ℹ schemas.ts | 100.00 | 100.00 | 100.00 |ℹ server.ts | 100.00 | 85.71 | 100.00 |ℹ sessions.ts | 100.00 | 100.00 | 100.00 |ℹ shop.ts | 100.00 | 100.00 | 100.00 |ℹ shutdown.ts | 100.00 | 100.00 | 100.00 |ℹ signature.ts | 100.00 | 100.00 | 100.00 |ℹ users.ts | 100.00 | 94.74 | 100.00 |ℹ v1.ts | 100.00 | 77.78 | 75.00 |ℹ v2.ts | 99.62 | 96.83 | 76.00 | 452-453ℹ ----------------------------------------------------------------------ℹ all files | 98.97 | 94.07 | 90.64 |ℹ ----------------------------------------------------------------------ℹ end of coverage reportBefore this, the 325 tests come out, one per line; here it starts from the summary. 325 are the 320 you counted in the introduction plus the 5 for the signature: the two helpers in test/ no longer count. It takes about two and a half minutes.
The last row: 98.97% of the lines, 94.07% of the branches and 90.64% of the functions. scripts/ and examples/ show up too, because some tests run them. If a few npm notice lines come out at the end, they’re npm’s: you don’t have to do anything.
Now, one row: db.ts, line 22, the file that opens the connections to PostgreSQL (Phase 5). Line 22 is the highlighted one:
// The database's address, from .env: postgres://user:password@server:port/databaseconst url = process.env.DATABASE_URL;if (url === undefined) { console.error('Configuration: DATABASE_URL: DATABASE_URL is missing (see .env.example)'); process.exit(1);}
// A pool: a few open connections that the requests borrow in turn. Opening a connection costs// (TCP, the password, setting up the session); reusing an open one, almost nothing. At most 10 at// a time: if more queries arrive, they wait their turn.export const pool = new Pool({ connectionString: url, max: 10 });
// If PostgreSQL closes a connection that was waiting in the pool (because it restarts, for// example), the pool reports it with an 'error'. With nobody listening, Node would stop the server.pool.on('error', (error) => { console.error('PostgreSQL closed a connection in the pool:', error.message);});It only runs if PostgreSQL drops a connection waiting in the pool, for example when it restarts, and no test restarts it. That’s why funcs % says 0.00: the file’s only function is that (error) => { … }.
Does it deserve a test? It would have to drop a pool connection from PostgreSQL’s side and check that the process is still alive. It can be done, and it costs. The line only writes a message; what matters is that, with someone listening to the 'error', Node doesn’t stop the process.
And here is coverage’s limit. Phase 9’s final review found the same bug in a borrowed connection, one that’s in use: nobody was listening to its 'error', and if PostgreSQL dropped it, the whole process stopped. The report couldn’t warn about it, because that line didn’t exist. The shop gets it in lesson 6.
Challenge: in heartbeat.ts, Phase 9’s worker heartbeat, lines 28 to 32 don’t run: what it does if it can’t write its file. How would you cover them? Is it worth it?Show answer
This one comes cheap. startHeartbeat receives the file to write to: a test can give it one inside a folder that doesn’t exist and, with mock.method(log, 'warn') (lesson 1), check that it warns only once even if it beats several times.
It’s worth it: it’s a unit test of milliseconds. Line 22 of db.ts costs much more: that’s why you decide row by row, not by the percentage.
On your server
Section titled “On your server”This lesson doesn’t change src/: there’s nothing to copy to your server. Don’t point BASE_URL at your server: production doesn’t load the practice accounts, and every run would leave a real order there.
You have already seen it
Section titled “You have already seen it”- A school fire drill: it doesn’t check an extinguisher, but that the whole building gets out in time. That’s why it’s done only now and then.
- GitHub Actions’ “Re-run failed jobs” button: the temptation with a flaky test.
- The “coverage 95%” badges on many projects on GitHub: now you know what they measure, and what they don’t.
Common mistakes
Section titled “Common mistakes”- Chasing 100% coverage: you end up with tests that run lines without checking anything.
- Testing everything end-to-end because “it’s the most real”: they take longer, fail for things outside your code and don’t say where.
- Unit tests with a fake database for bugs that live in the database: they pass green with the bug inside.
- Rerunning a flaky test until it passes, or skipping it: the bug is still there.
- Sleeping a fixed time instead of waiting for a condition with a maximum: on a slower machine, it isn’t enough.
- Running the end-to-end tests against production: they leave real orders, and they need an account with a known password.
Summary
Section titled “Summary”- Unit, one piece on its own; integration, several real ones together; end-to-end, the whole application from the outside. The more it covers, the more it catches and the more it costs.
- The pyramid asks for lots of unit tests; the trophy, mostly integration. The shop looks like the trophy because its bugs live in PostgreSQL.
npm run e2ebuys from the Compose shop over HTTP: it catches how the pieces fit together, and it needs them all running.- Coverage points to what nobody has run; it doesn’t say whether it’s checked, and it doesn’t see missing code.
- A flaky test depends on something it doesn’t control. You look for the cause; you don’t rerun it until it’s green.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- The Practical Test Pyramid, by Ham Vocke: the pyramid, with examples.
- The Testing Trophy and Testing Classifications, by Kent C. Dodds: the trophy.
- Collecting code coverage, in Node’s documentation.
- Test Coverage, by Martin Fowler: why a high number isn’t enough.
- Eradicating Non-Determinism in Tests, by Martin Fowler: flaky tests and how to get rid of them.