Writing
Test Cases
Learn to verify your JavaScript functions with assertions and build confidence that your code works exactly as expected.
What Is Testing?
Testing means running your code with known inputs and checking whether the outputs match what you expect. Instead of manually checking every result, you write small programs that verify correctness automatically.
Why Write Tests?
Anatomy of a Test Case
What Is an Assertion?
An assertion is a statement that declares something must be true. If the condition is true, nothing happens. If it is false, the assertion throws an error to tell you something is wrong.
// Built-in Node.js assertion const assert = require('assert'); // This passes silently assert.strictEqual(2 + 3, 5); // This throws an AssertionError! assert.strictEqual(2 + 3, 6);
Assertions vs console.log
| Assertion | console.log | |
|---|---|---|
| Purpose | Automated check | Manual inspection |
| On failure | Throws error | Prints value (you decide) |
| Scalable? | Yes: run 1000s | No: read each one |
| Reusable? | Yes | Not really |
The Core Pattern
// 1. The function you want to test function add(a, b) { return a + b; } // 2. Write test cases using assertions const assert = require('assert'); assert.strictEqual(add(2, 3), 5); // ✓ pass assert.strictEqual(add(0, 0), 0); // ✓ pass assert.strictEqual(add(-1, 1), 0); // ✓ pass console.log('All tests passed!');
Choosing Good Inputs
Don't just test the "happy path." Good tests cover edge cases and unusual inputs that might break your function.
How to Write a Test
assert.strictEqual(fn(input), expected)Example 1: isEven
function isEven(n) { return n % 2 === 0; } const assert = require('assert'); // Normal cases assert.strictEqual(isEven(4), true); assert.strictEqual(isEven(7), false); // Edge cases assert.strictEqual(isEven(0), true); assert.strictEqual(isEven(-2), true); assert.strictEqual(isEven(-3), false); console.log('isEven: all tests passed!');
Example 2: capitalize
function capitalize(str) { if (str.length === 0) return ''; return str[0].toUpperCase() + str.slice(1); } const assert = require('assert'); assert.strictEqual(capitalize('hello'), 'Hello'); assert.strictEqual(capitalize('a'), 'A'); assert.strictEqual(capitalize(''), ''); assert.strictEqual(capitalize('Hello'), 'Hello');
Example 3: maxOfThree
function maxOfThree(a, b, c) { return Math.max(a, b, c); } const assert = require('assert'); // Max in each position assert.strictEqual(maxOfThree(10, 5, 3), 10); assert.strictEqual(maxOfThree(1, 9, 4), 9); assert.strictEqual(maxOfThree(2, 6, 8), 8); // All equal assert.strictEqual(maxOfThree(5, 5, 5), 5); // Negative numbers assert.strictEqual(maxOfThree(-3, -1, -7), -1);
Testing Arrays and Objects
strictEqual checks with ===, which fails for arrays and objects (they compare by reference, not value). Use deepStrictEqual instead.
function double(arr) { return arr.map(x => x * 2); } const assert = require('assert'); // Use deepStrictEqual for arrays/objects assert.deepStrictEqual(double([1,2,3]), [2,4,6]); assert.deepStrictEqual(double([]), []); assert.deepStrictEqual(double([0]), [0]);
How a Test Runs
Common Mistakes
strictEqual(expected, actual) instead of strictEqual(actual, expected). The error message becomes confusing.strictEqual uses === on purpose. If you write your own check with ==, you might miss type mismatches.add(2, 3) and calling it done. Missing zeros, negatives, and edge cases leaves bugs hiding.Assertion Cheat Sheet
| Method | Use When | Example |
|---|---|---|
strictEqual |
Numbers, strings, booleans | strictEqual(add(1,2), 3) |
deepStrictEqual |
Arrays, objects | deepStrictEqual([1],[1]) |
ok |
Truthy check | ok(isEven(4)) |
throws |
Function should error | throws(() => div(1,0)) |
Question 1 of 5
What does assert.strictEqual(actual, expected) do when actual !== expected?
strictEqual throws an error that stops execution and shows you the actual vs expected values. It does not return a boolean or log a warning.Question 2 of 5
You want to test that reverseArray([1, 2, 3]) returns [3, 2, 1]. Which assertion should you use?
===, so strictEqual would fail even if the contents match. deepStrictEqual compares the contents element by element.Question 3 of 5
Given a function isPositive(n) that returns true if \( n > 0 \), which set of test inputs is most thorough?
Question 4 of 5
What is the correct order of arguments in assert.strictEqual?
strictEqual(actual, expected): the result of your function call comes first, the value you expect comes second. Swapping them makes error messages confusing.Question 5 of 5
Consider this function and test:
function greet(name) {
return 'Hello, ' + name + '!';
}
assert.strictEqual(greet('Alice'), 'Hello, Alice!');
assert.strictEqual(greet(''), 'Hello, !');
console.log('All tests passed!');
If the function is correct, what happens when this script runs?
greet('Alice') returns 'Hello, Alice!' and greet('') returns 'Hello, !'. Since no assertion fails, execution reaches the console.log and prints "All tests passed!"