16 min read
How to write tests in Node.js
In one sentence: A test is a piece of code that uses yours and checks a promise, like “with another secret, the signature isn’t valid.” In Node you write them with node:test and node:assert, with nothing to install, and you can only trust one once you’ve seen it fail.
What you will learn
- Write tests with node:test and node:assert from an empty file, in three parts: arrange, act and assert.
- Give each test a name that states its promise, and see it red before you trust its green.
- Prepare and clean up what the tests need with before and after, and group them with describe.
- Choose a test double (a fake gateway, mock.fn, mock.timers) and explain why the shop’s database isn’t faked.
Before you start, read: 00-introduction.md phase-4/07-webhooks.md phase-5/08-transactions.md
The problem
Section titled “The problem”This test of the shop is called “a signature made with the same secret is valid.” It signs a payment notification with the shop’s secret and checks that validSignature, the function that decides whether a notification comes from the payment gateway (Phase 4), says yes. It comes out green.
Now change validSignature so it says yes to everything, including a notification with no signature. That shop marks as paid the order of anyone who sends it a curl. And the test stays green: it checks that a good signature is valid, and that’s still true.
In Phase 3 we promised you’d learn to write the tests you were copying. Writing one is a few lines; the hard part is knowing whether it checks anything. Here you write the ones for validSignature from an empty file, watch each one fail, and break the quantity fix from the introduction to see which tests catch it.
The analogy
Section titled “The analogy”The analogy
On your ceiling there’s a smoke detector with a little green light. The light says it has a battery, not that it would beep if there were smoke. To find out, you press the test button: if it beeps, it works.
A green test is that light. Watching it fail on purpose is pressing the button. And what the box says, “detects smoke,” is its name: the promise it checks.
Where the analogy breaks down
- The button imitates smoke. To see a test red there’s no imitation: you really break the code, for a moment, and put it back as it was.
- A smoke detector doesn’t warn you about carbon monoxide, and nobody blames it: it doesn’t promise to. A test doesn’t catch what it doesn’t check either, even if it’s in the same function.
How it really works
Section titled “How it really works”What you’re going to test: the webhooks’ signature
Section titled “What you’re going to test: the webhooks’ signature”validSignature calculates the signature a payment notification should carry and compares it with the one it carries (Phase 4). The last line first checks that the two are the same size, because timingSafeEqual only compares things of the same size:
// It calculates the signature the request should carry and compares it with the one it carries.// timingSafeEqual takes the same time whether it matches or not: with ===, someone timing it could// guess it letter by letter.export function validSignature( body: string, signature: string | undefined, secret: string,): boolean { if (signature === undefined) return false; const expected = Buffer.from(sign(body, secret)); const received = Buffer.from(signature); // timingSafeEqual only compares two things of the same size. return expected.length === received.length && timingSafeEqual(expected, received);}It doesn’t use the database or the network, and with the same data it always returns the same thing. A test that tests a piece like that, on its own, is 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. Until now, the signature was only tested through the webhook’s route.
A test: a name and a function that checks
Section titled “A test: a name and a function that checks”This is test/signature.test.ts, the new test file of step 42. Start with what’s highlighted:
// The unit tests of the webhooks' signature (Phase 10, lesson 1). A unit test tests a single// function, with no database or server: that's why useDb() isn't needed and it runs in// milliseconds. sign is already tested in passing in webhooks.test.ts; here validSignature is// tested, the one that decides whether a notification comes from the gateway, with each promise// in its own test.import assert from 'node:assert/strict';import { test } from 'node:test';import { sign, validSignature } from '../src/signature.ts';
const SECRET = 'test-secret';const BODY = '{"id":"evt_1","type":"payment.completed","orderId":7}';
test('a signature made with the same secret is valid', () => { assert.equal(validSignature(BODY, sign(BODY, SECRET), SECRET), true);});
test('a signature made with another secret is not valid', () => { assert.equal(validSignature(BODY, sign(BODY, 'another-secret'), SECRET), false);});
test('a body changed in a single byte is not valid, even with the original signature', () => { const tampered = BODY.replace('"orderId":7', '"orderId":8'); assert.equal(validSignature(tampered, sign(BODY, SECRET), SECRET), false);});
test('without a signature it is not valid', () => { assert.equal(validSignature(BODY, undefined, SECRET), false);});
test('a signature of another size is not valid, and does not make timingSafeEqual fail', () => { assert.equal(validSignature(BODY, 'sha256=abc', SECRET), false); assert.equal(validSignature(BODY, '', SECRET), false);});Word by word:
import assert from 'node:assert/strict'brings in Node’s checks. With/strict, they compare strictly, like===in Phase 3’s lesson 2:9and'9'aren’t equal.import { test } from 'node:test'brings in the function that defines a test. Both come with Node (thenode:prefix): nothing to install.import { sign, validSignature } from '../src/signature.ts'brings in what you’re going to test, andsign, to make good signatures.SECRETandBODYare the test data.test('…', () => { … })defines a test: its name and a function with no name holding what it does.assert.equal(what comes out, what should come out)is an AssertionThe line of a test that compares what happened with what should have happened and makes the test fail if they differ. For example, checking that an order’s total is 25 and not 20.Go to definition: if they aren’t equal, it throws an error that shows both.
That’s a test: code that calls yours and checks what it returns. Node prints ✔ if the test’s function ends without an error, and ✖ if one is thrown.
Arrange, act, assert
Section titled “Arrange, act, assert”Almost every test has three parts, in this order: arrange the data, act (call what you’re testing) and assert the result. They’re known as the three A’s.
In short tests, like the signature ones, act and assert fit on one line. In the shop’s tests they’re easier to see. This is “asking for fewer units gives the difference back to the stock,” in test/transactions.test.ts:
- Arrange: it notes the mug’s stock,
before, and places an order for 5. - Act: it changes the order to 2, with a
PATCH, the way a customer would. - Assert a
200, that the order says 2 and that the stock isbefore - 2: the 3 extra ones have come back.
The name states the promise
Section titled “The name states the promise”Read the signature tests’ names: “a signature made with another secret is not valid,” “without a signature it is not valid.” Each one says what the code promises, with a concrete case, not “validSignature works” or “test 2.” It’s what you read when it fails, and it tells you what broke without opening the file.
The eleven tests of the quantity fix (pull request #48) were written like this, one per promise, like “changing the quantity and then cancelling leaves the stock as it was before the order.”
The Phase 4 one also said what it promised, “changes the quantity and returns the whole order”: read today, you can see it doesn’t mention the stock because it didn’t look at it.
One promise per test. A test stops at the first assertion that fails: if it checks five things, you learn nothing about the other four.
Other checks: deepEqual and rejects
Section titled “Other checks: deepEqual and rejects”assert.equal compares with ===: it works for numbers, text, true and false. Not for objects, because two different objects are never ===, even if they hold the same thing.
assert.deepEqual(a, b)compares the contents, field by field. The shop uses it to compare the whole body of a Problem Details error, with its four fields.await assert.rejects(promise, /message/)is for what has to fail and returns a promise: it passes if it fails with a message that matches, and fails if it goes well. “db.transaction: if the function throws an error, nothing it did is saved” checks that way that the transaction fails, and then that the stock is as before.
Red first: a test that has never failed proves nothing
Section titled “Red first: a test that has never failed proves nothing”A green test can pass because the code is right, or because it doesn’t look at what you think: the one in “The problem” passes with a validSignature that says yes to everything. The only way to know is to see it red once.
- You write the test
before trusting it
- You see it redthe code doesn’t exist yet, or you break it on purpose
you write the code, or put it back as it was
- You see it green
now it does
- Its green means something:what it promises holds
There are two ways to get to red:
- Write the test before the code, and then just enough for it to pass: test-driven development (TDD).
- Break the code on purpose, if it already exists: you remove what the test watches, see the
✖and put it back as it was.
Before and after the tests: before, after and describe
Section titled “Before and after the tests: before, after and describe”To have something ready before the tests, like a database with its tables, node:test brings four functions:
before(function): once, before the file’s first test.after(function): once, after the last one, to clean up.beforeEachandafterEach: before and after each test.
The shop has used them since Phase 5: almost all of its test files start with useDb(), from test/db.ts, a before that rebuilds the tests’ database, with its tables and the sample data, and an after that closes the connections.
Since all the files share that database, npm test runs the files one at a time (--test-concurrency=1).
describe('name', () => { … }) groups tests; the shop doesn’t use it, because it already has one file per topic.
Test doubles: fake pieces you already know
Section titled “Test doubles: fake pieces you already know”If what you’re testing talks to something slow, paid or out of your control, like a mail server or Google, in the test you put in its place a fake piece that behaves the same from the outside: a Test doubleA fake piece that takes the place of a real one during a test, like a “mail sender” that only writes down what it was asked to send instead of sending it. It avoids depending on slow, paid or unpredictable services.Go to definition. The course has already given you four:
- The payment gateway,
npm run gateway(Phase 4): it signs payment notifications without charging anyone. - Mailpit (Phase 9): a mail server that receives everything and delivers nothing.
- The practice OpenID Connect provider (Phase 6): it plays Google.
test/oauth.test.tsstarts it, and stops it with anafter. test/docker-fake.sh, since step 40: a fakedockerthat writes down every command ofdeploy.sh, to check in which order it swaps the copies without starting a single container.
Mocks and a fake clock
Section titled “Mocks and a fake clock”A MockA test double that gives canned answers and also remembers how it was used, so the test can check “it was called once with this value”. Use it with care: a test full of mocks checks the mock more than your code.Go to definition is a double that also writes down how it was used. node:test brings them in mock:
mock.fn()creates a fake function that writes down every call. You give it to the shop instead of, say, the one that notifies the delivery company, and check it was called once, with the right order.mock.method(object, 'name')wraps a function of an object: it keeps doing its job and writes down every call: a spy (if you give it another function, it replaces it). Intest/email.test.ts, “without SMTP_URL, it sends nothing and says so in the log” uses it withlog.infoand checks what it wrote.
mock.timers writes nothing down: it’s a fake clock, another double. The shop already has its own: createLimiter, in src/rate-limits.ts, receives where to get the time from, and test/rate-limits.test.ts passes it () => now; by changing now, a minute goes by instantly. mock.timers does the same with Date and setTimeout, without adding that parameter (Node’s documentation).
The database isn’t faked
Section titled “The database isn’t faked”You could do the same with PostgreSQL: a double that answers whatever stock each test says. It wouldn’t have caught the quantity bug.
That bug lived in the database: in which UPDATE was run, inside which TransactionA group of SQL commands that the database treats as one: either all of them are saved, or none. It starts with BEGIN and ends with COMMIT, which saves it, or with ROLLBACK, which undoes all of it.Go to definition and with which locks against Phase 5’s Race conditionA bug that only shows up when two operations run at the same time and the result depends on which one gets there first. For example, two buyers read that there’s one mug left, and both buy it.Go to definition. A double answers what you believed: it checks your idea of PostgreSQL, not PostgreSQL.
That’s why the shop’s tests talk to a real PostgreSQL, shop_test: they’re 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, and lesson 2 explains when each type pays off. The rule: doubles for what’s outside your shop, not for what you’re testing.
What changes in step 42
Section titled “What changes in step 42”test/signature.test.ts, the file above.npm testgives Node the pattern'test/*.test.ts', instead of letting it guess (the introduction’s challenge): a new test file has to be intest/and end in.test.ts.- That’s why
npm test -- test/signature.test.tsno longer runs a single file, as in Phase 5, but all of them. For just one:node --env-file=test/.env --test test/signature.test.ts.
Try it
Section titled “Try it”The goal: write the tests for validSignature from an empty file, watch each one fail, and break the quantity fix to find out which of the 18 transactions tests catch it.
The outputs are from Node 26, on Linux, on October 24, 2026; your times will be different. Where they say file:///p/, yours show your project’s folder (ours was in /p, in a container).
Get the step ready
Section titled “Get the step ready”Put your project on step 42, as in the introduction:
cd ~/backend-course/phase-3curl -O https://backenddesdecero.com/practicas/shop-api/42-tests.tar.gztar -xzf 42-tests.tar.gzcp -R 42-tests/. shop-api/cd shop-apinpm installFrom the 42-tests folder you’ll take the good code every time you break it. The transactions tests need PostgreSQL with shop_test (“Before you start”).
Write the signature tests from scratch
Section titled “Write the signature tests from scratch”Delete the step’s file: rm test/signature.test.ts. A copy stays in ../42-tests/test/signature.test.ts.
Create test/signature.test.ts, empty, in your editor. Write the three import lines, the secret, the test body and one test for each of these promises, with the promise as its name:
- a signature made with the same secret is valid;
- a signature made with another secret is not valid;
- a body changed in a single byte is not valid, even with the original signature;
- without a signature it is not valid (the signature is
undefined); - a signature of another size is not valid, and does not make
timingSafeEqualfail (try'sha256=abc'and'').
If you get stuck, look at the one above. Save, and don’t run it yet: first, break what it tests. In src/signature.ts, replace what’s between the braces of validSignature with a single line, return true;.
1. A validSignature that says yes to everything
node --env-file=test/.env --test test/signature.test.ts | head -n 36What you will see:
✔ a signature made with the same secret is valid (1.707553ms)✖ a signature made with another secret is not valid (0.666511ms)✖ a body changed in a single byte is not valid, even with the original signature (0.174306ms)✖ without a signature it is not valid (0.108925ms)✖ a signature of another size is not valid, and does not make timingSafeEqual fail (0.092883ms)ℹ tests 5ℹ suites 0ℹ pass 1ℹ fail 4ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 87.068714
✖ failing tests:
test at test/signature.test.ts:17:1✖ a signature made with another secret is not valid (0.666511ms)AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
true !== false
at TestContext.<anonymous> (file:///p/test/signature.test.ts:18:10) at Test.runInAsyncScope (node:async_hooks:226:14) at Test.run (node:internal/test_runner/test:1402:25) at Test.processPendingSubtests (node:internal/test_runner/test:974:18) at Test.postRun (node:internal/test_runner/test:1542:19) at Test.run (node:internal/test_runner/test:1467:12) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:387:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: true, expected: false, operator: 'strictEqual', diff: 'simple'}head -n 36 keeps the first 36 lines. Four out of five red. The one still green, “a signature made with the same secret is valid,” on its own would have passed a shop that accepts any notification.
In true !== false, what came out goes first and then what was expected, the order of assert.equal. test at test/signature.test.ts:17:1 says where the test starts, and the first at line, where the assertion is; your line numbers and your order will be different.
If you get five ✔, you didn’t save src/signature.ts or your tests don’t check what you think. If you get SyntaxError, the message says the line: almost always, a parenthesis or a brace left open.
Challenge: how would you see the first test red, the one that just passed?Show answer
With return false;: the first one fails, since it expects true, and the other four pass.
Put src/signature.ts back as it was: cp ../42-tests/src/signature.ts src/. Now break something else: in the last line of validSignature, remove expected.length === received.length && , so it reads return timingSafeEqual(expected, received);.
2. Without the size check
node --env-file=test/.env --test test/signature.test.tsWhat you will see:
✔ a signature made with the same secret is valid (1.824362ms)✔ a signature made with another secret is not valid (0.121802ms)✔ a body changed in a single byte is not valid, even with the original signature (0.134553ms)✔ without a signature it is not valid (0.072465ms)✖ a signature of another size is not valid, and does not make timingSafeEqual fail (0.129803ms)ℹ tests 5ℹ suites 0ℹ pass 4ℹ fail 1ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 84.421282
✖ failing tests:
test at test/signature.test.ts:30:1✖ a signature of another size is not valid, and does not make timingSafeEqual fail (0.129803ms)RangeError [Error]: Input buffers must have the same byte length at validSignature (file:///p/src/signature.ts:27:10) at TestContext.<anonymous> (file:///p/test/signature.test.ts:31:16) at Test.runInAsyncScope (node:async_hooks:226:14) at Test.run (node:internal/test_runner/test:1402:25) at Test.processPendingSubtests (node:internal/test_runner/test:974:18) at Test.postRun (node:internal/test_runner/test:1542:19) at Test.run (node:internal/test_runner/test:1467:12) at async Test.processPendingSubtests (node:internal/test_runner/test:974:7) { code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH'}Only the fifth one fails, with a RangeError from timingSafeEqual: a test also fails when the code throws an error. It guards the check you removed, and none of the other four notices it.
3. The code as it was: all five, green
cp ../42-tests/src/signature.ts src/node --env-file=test/.env --test test/signature.test.tsWhat you will see:
✔ a signature made with the same secret is valid (1.344657ms)✔ a signature made with another secret is not valid (0.130928ms)✔ a body changed in a single byte is not valid, even with the original signature (0.119385ms)✔ without a signature it is not valid (0.074423ms)✔ a signature of another size is not valid, and does not make timingSafeEqual fail (0.09755ms)ℹ tests 5ℹ suites 0ℹ pass 5ℹ fail 0ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 75.531411Five ✔, and now their green means something: you’ve seen each one fail. The file takes about 75 milliseconds: it doesn’t touch the database.
Break the quantity fix
Section titled “Break the quantity fix”Now, the tests of a real bug. In src/data.ts, look for async function changeQuantity: it has an if (difference > 0), for when the customer asks for more units, and an else if (difference < 0), for when they ask for fewer.
The else if has a single statement, four lines long, that starts with await tx: the UPDATE that gives the extra units back to the stock. Comment it out: // at the start of each of its four lines (in VS Code, select them and press Ctrl + /, or Cmd + / on a Mac). Save.
4. Asking for fewer no longer gives anything back to the stock
node --env-file=test/.env --test test/transactions.test.tsWhat you will see:
✔ 20 orders at once for a product with stock 5: 5 go through, 15 get 409, and the stock ends at 0 (124.544206ms)✔ without enough stock: 409, with its own type and what is left in detail (3.013434ms)✔ an order takes its quantity from the stock; cancelling it gives it back, only once (8.791113ms)✔ an error halfway through the transaction leaves the stock as it was (2.495429ms)✔ db.transaction: if the function throws an error, nothing it did is saved (1.195148ms)✔ asking for more units of a pending order takes the difference from the stock (5.655751ms)✔ asking for more units than are left: 409, with what is left, and neither the order nor the stock changes (5.577286ms)✖ asking for fewer units gives the difference back to the stock (6.309768ms)✖ changing the quantity and then cancelling leaves the stock as it was before the order (9.932006ms)✔ changing the quantity and cancelling at the same time: the stock ends as before the orders (52.576744ms)✔ an order that is no longer pending: 409, and neither the order nor the stock changes (5.397773ms)✔ paying and cancelling at the same time: a cancelled order does not go back to paid, and the stock comes back only once (58.973507ms)✔ paying and changing the quantity at the same time: the order ends paid with the final quantity, and the stock adds up (36.731941ms)✔ shipping and cancelling at the same time: a cancelled order does not leave, and only the stock of the cancelled ones comes back (41.543746ms)✔ cancelling and shipping at the same time: either it is cancelled and the stock comes back, or 409 and the order leaves as it was (39.843802ms)✔ if it is shipped while being cancelled, the cancellation changes nothing and answers 409 (5.958698ms)✔ examples/race.ts: reading and writing separately oversells; with a conditional UPDATE, it does not (605.598196ms)✔ the contract documents the 409 of POST /orders (11.85214ms)ℹ tests 18ℹ suites 0ℹ pass 16ℹ fail 2ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 2310.649153
✖ failing tests:
test at test/transactions.test.ts:145:1✖ asking for fewer units gives the difference back to the stock (6.309768ms)AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
30 !== 33
at TestContext.<anonymous> (file:///p/test/transactions.test.ts:151:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) at async Test.run (node:internal/test_runner/test:1409:7) at async Test.processPendingSubtests (node:internal/test_runner/test:974:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 30, expected: 33, operator: 'strictEqual', diff: 'simple'}
test at test/transactions.test.ts:154:1✖ changing the quantity and then cancelling leaves the stock as it was before the order (9.932006ms)AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
29 !== 30
at TestContext.<anonymous> (file:///p/test/transactions.test.ts:160:10) at process.processTicksAndRejections (node:internal/process/task_queues:104:5) at async Test.run (node:internal/test_runner/test:1409:7) at async Test.processPendingSubtests (node:internal/test_runner/test:974:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 29, expected: 30, operator: 'strictEqual', diff: 'simple'}Two out of eighteen red, and both ask for fewer units:
- “asking for fewer units gives the difference back to the stock,” with
30 !== 33: the order for 5 left the stock at 30, and going down to 2 should have given 3 back. - “changing the quantity and then cancelling…,” with
29 !== 30. Going from 4 to 3 should have given a mug back, and it didn’t.
The other sixteen don’t ask for fewer, and stay green. If all eighteen come out green, you didn’t save src/data.ts.
5. Back to green
cp ../42-tests/src/data.ts src/node --env-file=test/.env --test test/transactions.test.ts | tail -n 8What you will see:
ℹ tests 18ℹ suites 0ℹ pass 18ℹ fail 0ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 2242.289094tail -n 8 keeps the summary: pass 18, fail 0.
The real bug: the whole block
Section titled “The real bug: the whole block”What sat in the shop for twenty steps wasn’t that statement: it was that changeQuantity never moved the stock at all. To see it, comment out the whole block, from if (difference > 0) { to the brace that closes the else if, included.
6. Changing the quantity without touching the stock
node --env-file=test/.env --test test/transactions.test.ts | head -n 26What you will see:
✔ 20 orders at once for a product with stock 5: 5 go through, 15 get 409, and the stock ends at 0 (194.010655ms)✔ without enough stock: 409, with its own type and what is left in detail (4.837533ms)✔ an order takes its quantity from the stock; cancelling it gives it back, only once (10.838465ms)✔ an error halfway through the transaction leaves the stock as it was (3.672633ms)✔ db.transaction: if the function throws an error, nothing it did is saved (1.820252ms)✖ asking for more units of a pending order takes the difference from the stock (8.04554ms)✖ asking for more units than are left: 409, with what is left, and neither the order nor the stock changes (6.397178ms)✖ asking for fewer units gives the difference back to the stock (6.052021ms)✖ changing the quantity and then cancelling leaves the stock as it was before the order (10.538687ms)✖ changing the quantity and cancelling at the same time: the stock ends as before the orders (47.604383ms)✔ an order that is no longer pending: 409, and neither the order nor the stock changes (6.479727ms)✔ paying and cancelling at the same time: a cancelled order does not go back to paid, and the stock comes back only once (67.892977ms)✔ paying and changing the quantity at the same time: the order ends paid with the final quantity, and the stock adds up (38.442781ms)✔ shipping and cancelling at the same time: a cancelled order does not leave, and only the stock of the cancelled ones comes back (46.123661ms)✔ cancelling and shipping at the same time: either it is cancelled and the stock comes back, or 409 and the order leaves as it was (43.470012ms)✔ if it is shipped while being cancelled, the cancellation changes nothing and answers 409 (6.000185ms)✔ examples/race.ts: reading and writing separately oversells; with a conditional UPDATE, it does not (596.778433ms)✔ the contract documents the 409 of POST /orders (11.617959ms)ℹ tests 18ℹ suites 0ℹ pass 13ℹ fail 5ℹ cancelled 0ℹ skipped 0ℹ todo 0ℹ duration_ms 2300.551146Five out of eighteen red: the two from before, the two that ask for more units and “changing the quantity and cancelling at the same time.” With the bug that sold mugs that weren’t there, thirteen stay green.
Challenge: “paying and changing the quantity at the same time” checks that the stock adds up, and stays green with the stock broken. Why doesn’t it catch the bug?Show answer
It fires a payment and a change to 3 at the same time for ten orders of one mug. If the change gets there first, the stock doesn’t add up and the test comes out red; if the payment gets there first, the change gets a 409 and everything adds up. That time, the ten payments won. A test only catches what happens while it runs. On your computer some change may win, and then this one will come out red too.
Put src/data.ts back as it was (cp ../42-tests/src/data.ts src/) and repeat step 5: 18 out of 18.
On your server
Section titled “On your server”This lesson only changes tests: there’s nothing to copy to your server. If you push, the workflow from Phase 8 runs npm test, with yours, before publishing the image: if one fails, it isn’t published. Before the commit, git diff src/ shouldn’t show anything.
You have already seen it
Section titled “You have already seen it”- The test button on a smoke detector, or the TEST button on a bathroom outlet: checking that it trips, without waiting for the fire.
- The green check marks on a pull request on GitHub: tests someone wrote, run by a machine.
- Every
npm testin the course, since Phase 3: the files intest/you were copying were this.
Common mistakes
Section titled “Common mistakes”- Trusting a test you’ve never seen fail. It may be green because it looks at nothing.
- Names that say nothing, like “works” or “test 2,” or several promises in one test: when it fails, you don’t know what broke.
- Comparing objects with
assert.equal, or forgetting theawaitin front ofassert.rejects: the first fails even if they hold the same thing; the second passes without checking anything. - Faking the database. The double answers what you believe, not what PostgreSQL does.
Summary
Section titled “Summary”- A test calls your code and checks what it returns, with
node:testandnode:assert: arrange, act and assert. - Its name states the promise: it’s the first thing you read when it fails.
- A test you’ve never seen fail proves nothing: write it before the code, or break the code on purpose.
- Test doubles take the place of what’s outside the shop; the database, where its bugs live, is the real thing.
- A test only catches what it checks: with the stock completely broken, thirteen of the eighteen transactions tests stayed green.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- Test runner, in Node’s documentation:
test,describe,before,afterandmock. - Assert: every check in
node:assert, and what changes with/strict. - Mocks Aren’t Stubs, by Martin Fowler: the kinds of test doubles and when to use each one.
- Pull request #48 of the course’s repository: the quantity fix and its eleven new tests.