Design isolated unit tests and boundary cases
Quick answer
A unit test checks a small behavior in isolation. A good test states a contract, arranges inputs, invokes the unit and asserts an independently determined outcome.
How it works
Use node --test file.test.cjs to run a CommonJS test file using test from node:test and strict assertions. Choose typical inputs, empty input, boundary equality, invalid types and error paths according to the contract. A table of cases can reduce repetitive setup while keeping each case distinct. Test externally visible behavior instead of incidental internal steps. The snippets below execute assertions directly; the capstone runs the real test runner.
Local examples
Run in Node.js v22.16.0 as a CommonJS script. No interactive debugger attached; actual ByteMind editor support is unverified.
(async () => {const assert=require("node:assert/strict");const valid=n=>Number.isInteger(n)&&n>=0&&n<=10;const cases=[[-1,false],[0,true],[10,true],[11,false],[1.5,false]];for(const [n,want] of cases)assert.strictEqual(valid(n),want);console.log(cases.length);})()Expected console output:
5All five distinct boundary or invalid inputs pass.
Run in Node.js v22.16.0 as a CommonJS script. No interactive debugger attached; actual ByteMind editor support is unverified.
(async () => {const assert=require("node:assert/strict");function quantity(n){if(!Number.isInteger(n))throw new TypeError("integer required");return n;}assert.throws(()=>quantity(1.5),TypeError);console.log(quantity(0));})()Expected console output:
0The invalid input throws the required error type, while zero is valid.
Common mistakes
Do not compute expected values by calling the same implementation. A passing typical example does not establish boundary behavior.
Practice
For an inclusive quantity range 0–10, write separate named cases for -1, 0, 10, 11 and a fractional value.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Design isolated unit tests and boundary cases — Knowledge Check
Apply the concept and reason about local example output.
What does an independent expected value help prevent?
How should assert.throws receive a potentially throwing operation?
Why include a zero boundary test when zero is allowed?
What is the exact console output in Design isolated unit tests and boundary cases example 1?
(async () => {const assert=require("node:assert/strict");const valid=n=>Number.isInteger(n)&&n>=0&&n<=10;const cases=[[-1,false],[0,true],[10,true],[11,false],[1.5,false]];for(const [n,want] of cases)assert.strictEqual(valid(n),want);console.log(cases.length);})()What is the exact console output in Design isolated unit tests and boundary cases example 2?
(async () => {const assert=require("node:assert/strict");function quantity(n){if(!Number.isInteger(n))throw new TypeError("integer required");return n;}assert.throws(()=>quantity(1.5),TypeError);console.log(quantity(0));})()