What this test will do
A unit test checks a small piece of code in isolation: give a function an input, then verify that its output matches what you expect. For a first test, a function that adds two numbers is enough. The arithmetic is simple, so you can focus on how the test is written and run.
This example uses Node.js 22 or later, a terminal, and two files in the same folder. It uses Node’s built-in test runner and assertion module, so there are no packages to install. The files use the .mjs extension to make their JavaScript module format explicit. If you already have Node installed, check the version with node --version before continuing.
Create the function and its test
Make a folder for the example and create a file named sum.mjs. Give it this one-line content: export function sum(a, b) { return a + b; }. The export makes the function available to another module. In a larger project, this function would contain behavior worth checking; here, it gives the test a clear target.
In the same folder, create sum.test.mjs with these three statements: import { test } from 'node:test'; import assert from 'node:assert/strict'; import { sum } from './sum.mjs';. Then add the test: test('adds two numbers', () => { assert.equal(sum(2, 3), 5); });. You can put each statement on its own line. The test function names a case and runs its callback. The assertion compares the actual result, sum(2, 3), with the expected result, 5.
The ./ in the import path matters: it tells Node to load sum.mjs from the current folder. The .test.mjs suffix also matters because Node’s test runner recognizes it as a test file. Using node:assert/strict gives assert.equal strict equality behavior, so a number is not treated as equal to a string containing the same digits.
Run the test and read the result
Open a terminal in the folder containing both files and run node --test. Node finds sum.test.mjs, runs the named test, and prints a result. A passing result means this particular input produced the expected output; it does not prove the function is correct for every possible input.
To see a useful failure, temporarily change the expected value in the test from 5 to 6 and run node --test again. The assertion fails because the actual result is still 5. The output identifies the failing test and reports the values involved, helping you locate the mismatch. Change the expectation back to 5 and rerun the command to confirm the test passes. This deliberate failure is a quick way to verify that your test can detect a problem rather than merely run without errors.
Add cases that explain the behavior
One test covers only one example. Add another case to sum.test.mjs: test('adds a negative number', () => { assert.equal(sum(5, -2), 3); });. A second case checks behavior the original input did not exercise. You could also test zero with assert.equal(sum(0, 7), 7). Keep each test name descriptive enough that a failure points you toward the behavior to inspect.
Choose expectations from the intended behavior, not from what the current implementation happens to return. For instance, JavaScript’s + operator joins strings as well as adding numbers. If sum is supposed to accept only numbers, decide how it should handle other inputs before writing a test for them. Without that decision, a test for string inputs could accidentally endorse behavior you did not intend.
Use tests as a debugging aid
When a test fails unexpectedly, read the test name, the actual and expected values, and the location of the failure. Check that the expectation describes the behavior you want, then inspect the function. Change one thing at a time and run node --test again. A small, repeatable test makes it easier to tell whether your change addressed the problem.
As your project grows, keep tests near the behavior they cover and add cases for bugs you fix. Tests are most useful when they describe meaningful inputs and outcomes, rather than repeating the implementation step by step. This first example establishes the basic loop: write an expectation, run it, inspect the result, and refine the code or test when needed.



