By the end of this lesson
Use the built-in test runner and strict assertions.
Before you start
- Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
- Complete lesson 21 or be comfortable with its examples.
Three words to know
- Assertion
- A check that fails when the expected behaviour does not occur.
- Test case
- A named, repeatable behaviour check.
- Regression
- A behaviour broken after a later change.
1. Use Node’s built-in runner
Save the file as lesson.test.mjs. node:test and node:assert/strict are included with Node 24; there is no npm package to install for this lesson. Tests are named so a failure tells you which behaviour needs attention.
import test from "node:test";
import assert from "node:assert/strict";
function countOpen(statuses) {
return statuses.filter(status => status === "open").length;
}
test("counts exact open values", () => {
assert.equal(countOpen(["open", "closed", "open"]), 2);
});
test("an empty list has zero matches", () => {
assert.equal(countOpen([]), 0);
});
test("status matching is case-sensitive", () => {
assert.equal(countOpen(["Open"]), 0);
});
2. Run and deliberately break one expectation
node --test lesson.test.mjs
What you should see · example output
Three tests pass. Exact reporter formatting can differ.
Change the first expected value from 2 to 3 and run again. Observe the failure, then restore 2. Do not rewrite an expectation solely to hide a defect; decide the intended contract first. A nonzero test-runner status allows automation to stop after failure.
3. Cover a meaningful boundary
A passing suite covers the cases you wrote, not every possible input. Include invalid types, empty data, limits and an invalid last record that must not produce partial stdout. The final project tests the real process as well as the pure summarise function. Testing output, stderr and exit status together is stronger than merely looking for an error word in a log.
YOUR TURN
A small check before you continue
Add a check for ["closed", "closed"]. What result should it expect?
Show a suggested answer
It should expect 0 because the function counts only exact open matches, not all records.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
