Lesson 26 of 28

In progress

Course progress

JavaScript

A first automated test

Write a test for a pure function, run it with Node, and learn what is worth testing before adding a framework.

A test is a small program that checks another program. The first one is usually uncomfortable to write and immediately useful: it turns "I think this still works" into a fact you can recheck in a second.

What you will learn

  • Write a test with a plain assertion.
  • Choose cases that catch real bugs, including boundaries.
  • Run tests with Node’s built-in runner.

Start with a pure function

Pure functions are the easiest to test: same input, same output, no DOM and no network.

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

The assertion throws on failure, so the script stops with a message that names the case. That is the whole idea; frameworks add reporting, not truth.

Cases that catch bugs

A test that only checks the happy path proves very little. For a function that takes a number, the interesting cases are:

  • A typical value.
  • Zero.
  • A negative value.
  • The smallest and largest allowed values.
  • A value that should be rejected.
JavaScript
Editable. Runs in your browser.





Press Run to see the output.

A table of cases makes the boundaries visible and adding a new case is one line. That format is worth using even after you adopt a test runner.

Node’s test runner

Node ships with node:test and node:assert, so a project needs no dependency to start.

// math.test.js
import test from "node:test";
import assert from "node:assert/strict";
import { add } from "./math.js";
test("adds two numbers", () => {
assert.equal(add(2, 3), 5);
});
test("handles negative numbers", () => {
assert.equal(add(-2, -3), -5);
});

Run it with node --test. Node finds files matching *.test.js and reports pass and fail counts. assert.equal is strict in the strict import, and assert.deepEqual compares objects and arrays by content.

For DOM tests, a runner with a simulated DOM is needed; that is a later step, and it is worth delaying until the pure logic is covered.

Test behaviour, not implementation

A test that checks how a function works internally breaks when you refactor, even though behaviour is unchanged. A test that checks the return value survives.

Test the contract: given this input, this output. Do not assert on private variables, call counts of internal helpers, or exact DOM structure that a redesign will change.

Tests as documentation

A test file is executable documentation. it("rejects a negative quantity") states a rule in the same words a reader would use, and it cannot go stale the way a comment can, because the suite fails when the rule changes.

That makes naming important. A test called works documents nothing; returns 0 when the cart is empty tells the next person what the app promises. When you join a project, reading the test names is often faster than reading the source.

When a test fails

A red test means one of three things: the code is wrong, the test is wrong, or the requirement changed. Decide which before editing anything.

  • If the code is wrong, fix the code and leave the test alone.
  • If the test encoded an old requirement, update the test and say why in the message.
  • If the test is flaky, find the shared state or timing assumption, not a longer timeout.

Deleting a failing test to get a green run hides the bug it was written for. The only good reason to delete one is that the behaviour no longer exists.

What not to test

  • Third-party libraries; they have their own tests.
  • Trivial getters and setters.
  • Exact copy in the markup, which changes constantly.
  • Everything at once: a suite that takes ten minutes stops being run.

Test the logic with rules, the boundaries, and the bugs you have actually fixed. A regression test named after a real failure is worth ten speculative ones.

A test that catches every error and passes anyway is worse than no test. try { run(); } catch (e) {} with no assertion always succeeds. Let the failure throw, or assert on the error explicitly.

Write tests for a slugify function that lowercases, trims, replaces spaces with hyphens and strips punctuation. Include a normal title, leading spaces, repeated spaces and an empty string.

Show solution
JavaScript
Editable. Runs in your browser.





Press Run to see the output.

The cases cover the normal path, trimming, collapsed spaces, punctuation and the empty string. That is enough to catch the usual regressions without testing the regular expression itself.

Recap

  • An assertion throws on failure; a test is that plus a case worth checking.
  • Cover boundaries and previously fixed bugs, not just the happy path.
  • Test observable behaviour, and start with Node’s built-in runner.

Next, you will measure and improve page performance with the numbers the browser already reports.

How many tests should a project have?
Enough to cover the rules and the bugs that mattered. Coverage percentage is a rough signal, not a goal.
Do I need a framework?
Not to start. `node:test` covers functions and modules. Reach for a browser runner when you need to test DOM behaviour.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.