19 November 2026

Node.js 26 LTS for Students: Test JavaScript with node:test

  Asp.Net CS By Example       19 November 2026

Node.js 26 became a Long Term Support (LTS) release in October 2026. For a student project, LTS is a sensible line to install because it receives maintained releases. This lesson uses Node's built-in test runner to check a tiny grade calculator without Jest or another test package. The test runner is not new in Node 26; the new LTS release is a good time to learn a tool already included with Node.

Test workflow

grade.mjs → grade.test.mjs → node --test → Pass or fail report

Check Node and make two files

Install the current Node.js 26 LTS update from the official download page. In a terminal, run node --version. Make an empty folder named student-grades. Save the next two files there. The .mjs extension tells Node to use JavaScript modules, so these examples need no package configuration.

File 1: grade.mjs

The average function checks its input, then divides the total by the number of marks. passed uses that result and a default pass mark of 40.

export function average(marks) {
  if (!Array.isArray(marks) || marks.length === 0) {
    throw new TypeError('Add at least one mark');
  }
  if (!marks.every(mark => Number.isFinite(mark) && mark >= 0 && mark <= 100)) {
    throw new RangeError('Marks must be numbers from 0 to 100');
  }
  return marks.reduce((total, mark) => total + mark, 0) / marks.length;
}

export function passed(marks, passMark = 40) {
  return average(marks) >= passMark;
}

File 2: grade.test.mjs

test names each check. assert compares the actual result with the expected result. The last two checks confirm that bad input produces a clear error.

import test from 'node:test';
import assert from 'node:assert/strict';
import { average, passed } from './grade.mjs';

test('average of two marks is 60', () => {
  assert.equal(average([50, 70]), 60);
});

test('average of 40 meets the default pass mark', () => {
  assert.equal(passed([30, 50]), true);
});

test('an empty mark list is rejected', () => {
  assert.throws(() => average([]), TypeError);
});

test('out-of-range marks are rejected', () => {
  assert.throws(() => average([40, 120]), RangeError);
});

Run and read the result

From the folder containing both files, run:

node --test

Node discovers grade.test.mjs. The report should show 4 tests, 4 passed, 0 failed. Timing and symbols may differ between versions and terminals.

Change the first expected average from 60 to 61 and run again. One failing test shows how a test catches a wrong expectation. Restore 60 afterward.

Why this helps

  • Tests are small repeatable examples of what your function should do.
  • Input checks are as important as successful cases: an empty list must not silently return an invalid average.
  • No download from npm is needed for node:test or node:assert/strict.

Practice

  1. Add a test for passed([35, 40], 40). Predict the result before running.
  2. Add a test that accepts a mark of exactly 100.
  3. Change the pass mark to 50 in one call. Which tests still pass?

Continue with the Node.js learning path. Read the official test runner guide and the Node 26 LTS announcement for release details.

logoblog

Thanks for reading Node.js 26 LTS for Students: Test JavaScript with node:test

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.