Sub topic 11
Testing the Core Logic
Assume you know basic TypeScript, async/await, and how the project compiles with tsc. This chapter assumes that the command-line project already uses Node.js ESM and has a src directory.
A command-line tool usually combines two kinds of code:
- Process code reads arguments, opens files, writes output, and sets the exit code.
- Application logic transforms input into a result.
Test the application logic separately. Pure functions are easier to test because they do not depend on files, terminals, or the current process.
Keep the Counting Function Pure
Put the counting rules in src/count.ts. The function receives a string and returns an object. It does not read a file or print anything.
export function countText(text: string): { lines: number; words: number } {
const lines =
text === "" ? 0 : text.split("\n").length - (text.endsWith("\n") ? 1 : 0);
const words = text.split(/\s+/).filter(Boolean).length;
return { lines, words };
}This function treats a final newline as a line ending, not as an extra empty line. For example, "one\ntwo\n" contains two lines. The regular expression \s+ matches one or more whitespace characters, so spaces, tabs, and newlines separate words.
The function has no side effects. Calling it with the same input always produces the same result. This property makes it suitable for unit testing.
Write Tests with `node:test`
Node.js includes a test runner named node:test. Use strict assertions to compare the actual result with the expected result.
Create src/count.test.ts:
import { test } from "node:test";
import assert from "node:assert/strict";
import { countText } from "./count.js";
test("counts lines and words", () => {
assert.deepEqual(countText("a b\nc\n"), {
lines: 2,
words: 3,
});
});
test("empty input has no lines or words", () => {
assert.deepEqual(countText(""), {
lines: 0,
words: 0,
});
});
test("whitespace does not create words", () => {
assert.deepEqual(countText(" alpha\tbeta\n"), {
lines: 1,
words: 2,
});
});test registers a test case. assert.deepEqual compares the fields and values inside objects. If the values differ, the test runner reports a failure and exits with a nonzero status.
With module: "NodeNext", the import uses ./count.js even though the source file is named count.ts. TypeScript changes the extension when it emits JavaScript, so the import must describe the compiled file.
Run the Tests
A package script can compile the TypeScript files and then run the compiled tests:
{
"scripts": {
"build": "tsc",
"test": "tsc && node --test dist"
}
}Run it from the project directory:
npm testThe first command catches TypeScript errors. The second command runs JavaScript files in dist. Keeping compilation before testing prevents tests from using stale output.
Follow the Test Path
The test flow should avoid the process entry point:
The command entry point still needs separate testing for argument validation and file errors. However, those tests require process or file-system setup. Testing the pure function first gives fast feedback about the main counting rules.
Exercise
What test input would check that a final newline does not add an extra line?
Show answer
Use an input such as "first\nsecond\n" and expect lines to equal 2.