Skip to content

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

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

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.

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:

src/signature.ts
// 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.

This is test/signature.test.ts, the new test file of step 42. Start with what’s highlighted:

test/signature.test.ts
// 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: 9 and '9' aren’t equal.
  • import { test } from 'node:test' brings in the function that defines a test. Both come with Node (the node: prefix): nothing to install.
  • import { sign, validSignature } from '../src/signature.ts' brings in what you’re going to test, and sign, to make good signatures.
  • SECRET and BODY are 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.

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:

  1. Arrange: it notes the mug’s stock, before, and places an order for 5.
  2. Act: it changes the order to 2, with a PATCH, the way a customer would.
  3. Assert a 200, that the order says 2 and that the stock is before - 2: the 3 extra ones have come back.

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.

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.

  1. You write the test

    before trusting it

  2. 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

  3. You see it green

    now it does

  4. 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.
  • beforeEach and afterEach: 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.ts starts it, and stops it with an after.
  • test/docker-fake.sh, since step 40: a fake docker that writes down every command of deploy.sh, to check in which order it swaps the copies without starting a single container.

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). In test/email.test.ts, “without SMTP_URL, it sends nothing and says so in the log” uses it with log.info and 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).

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.

  • test/signature.test.ts, the file above.
  • npm test gives Node the pattern 'test/*.test.ts', instead of letting it guess (the introduction’s challenge): a new test file has to be in test/ and end in .test.ts.
  • That’s why npm test -- test/signature.test.ts no 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.

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).

Put your project on step 42, as in the introduction:

Ventana de terminal
cd ~/backend-course/phase-3
curl -O https://backenddesdecero.com/practicas/shop-api/42-tests.tar.gz
tar -xzf 42-tests.tar.gz
cp -R 42-tests/. shop-api/
cd shop-api
npm install

From 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”).

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:

  1. a signature made with the same secret is valid;
  2. a signature made with another secret is not valid;
  3. a body changed in a single byte is not valid, even with the original signature;
  4. without a signature it is not valid (the signature is undefined);
  5. a signature of another size is not valid, and does not make timingSafeEqual fail (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

Ventana de terminal
node --env-file=test/.env --test test/signature.test.ts | head -n 36

What 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

Ventana de terminal
node --env-file=test/.env --test test/signature.test.ts

What 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

Ventana de terminal
cp ../42-tests/src/signature.ts src/
node --env-file=test/.env --test test/signature.test.ts

What 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.531411

Five ✔, and now their green means something: you’ve seen each one fail. The file takes about 75 milliseconds: it doesn’t touch the database.

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

Ventana de terminal
node --env-file=test/.env --test test/transactions.test.ts

What 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

Ventana de terminal
cp ../42-tests/src/data.ts src/
node --env-file=test/.env --test test/transactions.test.ts | tail -n 8

What you will see:

ℹ tests 18
ℹ suites 0
ℹ pass 18
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 2242.289094

tail -n 8 keeps the summary: pass 18, fail 0.

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

Ventana de terminal
node --env-file=test/.env --test test/transactions.test.ts | head -n 26

What 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.551146

Five 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.

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.

  • 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 test in the course, since Phase 3: the files in test/ you were copying were this.
  • 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 the await in front of assert.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.
  • A test calls your code and checks what it returns, with node:test and node: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.
You write a test for a new function, priceWithTax, and it comes out green the first time. What do you do before trusting it?

Three names for the same test of the shop. Which one helps you most the day it comes out with ✖?

The test assert.equal(await res.json(), { id: 7, status: 'paid' }) fails, and the response carries exactly { id: 7, status: 'paid' }. Why?

A cancellation test fails with 12 !== 10 on the line assert.equal(await stock(4), before). What happened?

Someone on your team proposes replacing PostgreSQL, in the tests, with a double that answers whatever stock each test says: they’d run ten times faster. What would happen to the quantity tests?

Someone on your team adds a test, “paying for an order leaves it paid,” that pays and only checks that the response is 200. Will it catch a bug that leaves the order “pending”?

  • Test runner, in Node’s documentation: test, describe, before, after and mock.
  • 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.
phase-10 · lesson 1/7