Loading video player…

Running Your First Test With node --test

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:

Language: JavaScript
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:

Language: Shell
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:

Language: Shell
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.

00:00 Kirsty checks Snowline Hire’s fitting rules before changing them. Her teammate has ported the module to JavaScript. You need the same protection. In Python, this test asks for Morag’s ski length: 168 centimeters, intermediate ability.

00:17 It expects 155. Pytest finds this test function by its name. In JavaScript, the port is called skiLength(), using camelCase. You’ll check that same result.

00:31 The supplied package setting makes these JavaScript files modules. Node includes the test runner; there’s no testing package to install. Import the named test tool from node:test.

00:44 The braces select that name. Import the default assertion tool from node:assert/strict. The node: prefix means a built-in module.

00:56 Import skiLength() from the neighboring fitting.js file. Dot slash means this folder; include the file extension.

01:06 In JavaScript, call test() once for each check. The first argument is a readable name for the report. The second argument is a function for the runner to execute.

01:18 Empty parentheses mean no parameters. The arrow leads to its body, between braces. The closing symbols finish that body, the test() call, and the statement.

01:29 Python’s named test function serves the same purpose; here, you hand the runner a function.

01:37 Inside the function, call skiLength() with Morag’s height and ability. Give that result to assert.strictEqual() as the actual value, followed by the expected 155.

01:51 This assertion uses JavaScript’s strict equality, with no type conversion. A different value fails the check. For this number, it protects the same rule as Python’s assert.

02:03 Save the JavaScript check as test_fitting.js, following Python’s filename. Run node --test from the project folder. The extra reporter option chooses this readable report.

02:19 The code and filename are both visible. Your turn: pause the video and predict how many tests this command will report.

02:33 Zero tests. No failures, but nothing ran. Like pytest, the runner discovers files by name. Its patterns differ. test_fitting.js is outside these patterns, so the runner never reaches your test() call.

02:51 Use fitting.test.js beside fitting.js. Rename only the file; keep the check. Run the command again. This time, the named test passes.

03:06 The summary says tests one, pass one, fail zero. That confirms the check actually ran. The times beside the test and at the bottom vary between runs. The important result is one executed check, passing.

03:24 To run one file explicitly, put its filename after the options. In Python, you can likewise give pytest a test file. This runs the same named check and reports one pass.

03:39 Discovery chooses files for you; an explicit filename chooses this file. The assertion still decides whether its test passes.

03:49 If the expected length is 156 instead, the same check fails.

03:56 The failure shows the actual 155 and expected 156. This is an excerpt; the location and stack are omitted. The command’s exit status is one for this failure, zero for the passing run.

04:11 Check it immediately using your shell’s command. Automated checks use that status too. Restore the expected 155.

04:21 You now have one named test in fitting.test.js, using the runner that comes with Node. Read the test count as well as failures: zero failures alone can mean nothing ran.

04:35 Next up: Comparing Values and Objects in Assertions. You’ll check the whole fitting result, where comparing objects needs a different assertion.

Become a Member to join the conversation.