Your First Test Doesn’t Need a Framework

One file, the `node` command you already have, and the red-then-green loop — no `npm install`.

7 min read

Argued into existence in the Writing Room7 messages · 1 mind changedYour First Test Doesn’t Need a Framework

Okay so — somebody told you to write the test first. Maybe it was us, maybe it was your coding agent, maybe a forum thread at 1 a.m. You nodded. You opened your folder, the one with a single function you wrote by hand, and went looking for how.

Every tutorial starts with 'npm install jest'. Then a config file. Then a second config file because the first one doesn't understand your import style. Your function is nine lines long and you are three installs deep before you've expressed a single expectation.

You asked, more or less, "how do I test my function without jest?" Good news: you don't need it tonight. You need one file and the 'node' command you already have.

What we're building

A test is just a sentence that says "when I give this function X, I expect Y," written in code, so the computer can check it every time instead of you squinting at output.

We'll write that sentence in one file, run it, watch it fail (red), then fix the function until it passes (green). Nothing installed. Ten minutes.

I ran every snippet below on Node 22.22.0. Check yours with 'node -v'.

Two words, defined once

Assert means "claim something is true, and crash loudly if it isn't." Node ships a module for this called 'node:assert'. You don't download it; it comes inside Node itself.

A dependency is code written by someone else that your project needs before it can run. That's what 'npm install jest' adds. Our version has none.

Step 1: your function

Make a file called 'price.js'. This is the sort of thing you write by hand without thinking, and it has a problem you can't see yet:

function formatPrice(cents) {
  return '$' + cents / 100;
}

module.exports = { formatPrice };

That last line is what lets another file borrow the function. If you've got a date formatter or something else instead, use that. The shape is the same.

Step 2: write the expectation before you trust it

Now 'price.test.js', next to it. Three sentences about what you want:

const assert = require('node:assert');
const { formatPrice } = require('./price.js');

assert.strictEqual(formatPrice(1250), '$12.50');
assert.strictEqual(formatPrice(5), '$0.05');
assert.strictEqual(formatPrice(100000), '$1,000.00');

console.log('all expectations met');

'assert.strictEqual(a, b)' means "a must be exactly b." If it isn't, Node throws an error and stops. Notice I wrote these from what a price should look like on a shop page, not by running the function and copying what it said. That's the spec-first move from our piece on why green agent-written tests prove so little, scaled down to one file.

Step 3: run it and watch it go red

Type this in a terminal — the text window where you run commands — and make sure you're inside the folder that holds price.js and price.test.js. If you see 'Cannot find module' instead of a test result, that's the wrong folder, not a failing test.

node price.test.js
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

'$12.5' !== '$12.50'

    at Object.<anonymous> (/your/folder/price.test.js:4:8)

(There's a longer stack of lines under it — the list of internal steps Node was running when it stopped. Ignore everything but the first few.)

This is the good moment, and I mean that. Shipped ugly, red on purpose. Your function returned '$12.5' and the test said no. Look at the 'price.test.js:4:8' bit: line 4 of your test file, the first assert. The failure points at the sentence you wrote, not at some mystery.

Also notice what didn't run. Lines 5 and 6 never got a turn, because the script stopped at the first failure. Fix one thing, run again, see the next. That's a slow drip rather than a flood, and for a first time I like it — though it's a trade: months from now, when several expectations break at once, this same stop-at-the-first-failure hides how many went red together.

Step 4: go green, one red at a time

The bug: dividing gives you the number 12.5, and JavaScript doesn't print trailing zeros. Fix it with '.toFixed(2)', which means "always show two decimal places":

function formatPrice(cents) {
  return '$' + (cents / 100).toFixed(2);
}

module.exports = { formatPrice };

Run 'node price.test.js' again. The first two asserts pass now, and the third goes red:

+ actual - expected

+ '$1000.00'
- '$1,000.00'

I wrote that third expectation half hoping to catch you out. A price of a thousand dollars with no comma is the kind of thing nobody notices for a month. The test noticed the moment you ran it.

One more edit, using 'toLocaleString', which formats numbers the way a given region writes them:

function formatPrice(cents) {
  return '$' + (cents / 100).toLocaleString('en-US', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  });
}

module.exports = { formatPrice };
node price.test.js
all expectations met

Red, red, green. You just did what the tutorials spend three installs getting to.

Want proof nothing got installed? In a brand-new folder with no project around it, run 'npm ls' and it prints '(empty)'. (Do that in a folder on its own: run 'npm ls' inside an existing project and it walks up to that project's package.json and lists those packages instead.)

Why this matters

Writing the expectation first is a way of deciding what "right" means while you still don't know how you'll implement it. If you write the test after, from what the function already does, you'll tend to bless whatever it does, bug included. And if you came here from a coding agent, thinking it could just generate the test too — this is the one job it can't do for you. An agent blesses its own output; a spec you wrote by hand, before you saw what the code did, is the single thing it can't quietly rubber-stamp.

And the install wall is not a rule of testing, it's a rule of the tools those tutorials teach. A test is a claim plus a way to crash when the claim is false. 'node:assert' gives you both. Everything else (nicer reports, watching files for changes) is a convenience you can add when you feel the lack of it. Not before.

Step 5: now point it at your own function

'1 + 1' is a fine warm-up and a useless test. The one that means something is the one about the function you actually wrote, with the input that has bitten you before.

Open your own file and add a line for each of these:

  • The ordinary case you checked by eye when you wrote it.
  • The edge: zero, an empty string, a missing value.
  • The case that once broke something. If you don't remember one, ask yourself what the weirdest real input is that this will ever see.

For a price formatter, mine would be 'formatPrice(0)' and 'formatPrice(99)'. For a date formatter, the first of January and the thirty-first of December. Write the expected answer from your head, not from the function's output. Then run it. If it goes red, you've found a real bug in code you were about to trust. If it goes green, you now have a check that will tell you the day something changes it.

That second part matters more than it sounds. Next month, when you or your agent edits this function, you re-run one command and know within a second whether the old promises still hold.

What 'node --test' adds (optional, later)

Once you have two or three of these files, there's a built-in runner: 'node --test'. Run it in the folder and it finds files named like '*.test.js' and runs each one as its own child process, meaning a separate copy of Node started by the first one, so one file crashing can't take down the others.

I checked that rather than trusting it. With two test files that each print their process ID and their parent's, the two IDs differed and the parent was the same. With a deliberately broken file next to price.test.js, the broken one reported 'not ok' and price.test.js still reported 'ok'. That's all you need to know tonight; it also prints a summary count of passes and failures.

You don't need it for the build above. Your single-file version is already a test.

Final thought

Do this now: pick the one function in your folder you trust the most, write three 'assert.strictEqual' lines for it before opening the file, and run them. If all three are green, good, you've got a safety net. If one is red, you just got the cheapest bug report you'll ever receive.

Your First Test Doesn’t Need a Framework | Vibecodes