A passing command is useful only when it actually ran your check. For Snowline Hire, one test protects the rule that Morag’s height and ability produce 155 cm skis.
Node includes a test runner and assertion tools. In the supplied project, package.json contains "type": "module", so the .js files use module imports. Save this check as fitting.test.js, beside the supplied fitting.js:
import { test } from "node:test";
import assert from "node:assert/strict";
import { skiLength } from "./fitting.js";
test("ski length rounds down to stock", () => {
assert.strictEqual(skiLength(168, "intermediate"), 155);
});
test() takes a readable report name and a function for the runner to execute. The empty parentheses before the arrow mean the function takes no parameters. assert.strictEqual() compares its first argument, the actual result, with its second, the expected value, using strict equality without type conversion.
Run the test from your project folder. The reporter option selects the readable report used in the video:
node --test --test-reporter=spec
Check that the summary reports tests 1, pass 1, and fail 0. Durations vary between runs. A Python-style filename such as test_fitting.js is not discovered by this command: tests 0 and fail 0 mean nothing ran.
The runner recognizes names such as *.test.js, *-test.js, *_test.js, test-*.js, test.js, and JavaScript test files in a test folder. This project uses fitting.test.js. To select that file explicitly, include its name:
node --test --test-reporter=spec fitting.test.js
A failed numeric comparison shows the actual and expected values. The demonstrated failure returns exit status 1; the passing run returns 0. Immediately after running the test command, read its status using your shell:
| Shell | Command |
|---|---|
| macOS or Linux shell | echo $? |
| PowerShell | $LASTEXITCODE |
| Windows Command Prompt | echo %errorlevel% |
The zero-test run also returns 0, so read the test count alongside the status. Next up: Comparing Values and Objects in Assertions, where you’ll check the whole fitting result.