Node.js Test Runner: Test Course Filters Without a Framework
Practical Web Development — Lesson 7 of 10 · Intermediate
A small pure function is a useful first testing target: it takes data and returns a result without network or browser dependencies. This lesson separates course filtering from the interface and tests the behaviour using Node's built-in test runner. Use a currently supported Node.js release and save the files with the .mjs extension.
Define the behaviour first
An empty or whitespace-only query returns every course. Matching ignores case. A query that matches nothing returns an empty array. The function should not reorder or change the caller's input array. These rules are a contract a user can observe, rather than a list of implementation details.
Create filter.mjs:
export function filterCourses(courses, query) {
const term = query.trim().toLowerCase();
return courses.filter(course => course.title.toLowerCase().includes(term));
}Create filter.test.mjs in the same folder:
import test from 'node:test';
import assert from 'node:assert/strict';
import { filterCourses } from './filter.mjs';
const courses = [
{ id: 'html', title: 'HTML fundamentals' },
{ id: 'css', title: 'CSS layout' }
];
test('matches without case sensitivity and trims the query', () => {
assert.deepEqual(filterCourses(courses, ' cSs ').map(x => x.id), ['css']);
});
test('blank query includes all courses', () => {
assert.deepEqual(filterCourses(courses, ' '), courses);
});
test('unknown term returns no matches', () => {
assert.deepEqual(filterCourses(courses, 'database'), []);
});
test('does not mutate the input', () => {
const snapshot = structuredClone(courses);
filterCourses(courses, 'html');
assert.deepEqual(courses, snapshot);
});Run node --test filter.test.mjs from that folder. All four tests should pass. Change includes to startsWith and add a test searching for “layout”; that new test should expose the changed behaviour.
Use failures as useful feedback
A failing assertion should tell you which promise the function broke. Prefer names that describe outcomes. Avoid checking which array method was used: an equally correct implementation should pass. When a real bug appears, add the smallest test that reproduces its user-visible effect before fixing it.
Keep the boundary explicit
This function assumes an array of objects with string titles and a string query. Validate API data before calling it, as in the earlier TypeScript boundary lesson. Testing this pure function does not prove that the browser reads the input field correctly, renders an empty state or handles a failed request. Those need integration or browser tests.
Practice
Add an optional level filter and write cases for all levels, one known level and an unknown level. Decide whether an unknown level returns no courses or falls back to all before you write the implementation. That design decision matters more than the number of assertions.
Continue learning
Review our semantic HTML project, JavaScript course filter and course platform architecture.
Reference: Technical documentation for this lesson. Examples are learning exercises; test them in your environment.
Comments
Post a Comment