node:test in JavaScript
The runner is built in: a test function and the assert module.
import test from "node:test";
import assert from "node:assert/strict";
function slugify(title) {
return title.trim().toLowerCase().replaceAll(" ", "-");
}
test("joins words with hyphens", () => {
assert.strictEqual(slugify("Language Basics"), "language-basics");
});
test("trims the edges", () => {
assert.equal(slugify(" Async "), "async");
});
test("leaves one word alone", () => {
assert.ok(!slugify("basics").includes("-"));
});
How it works
test(name, fn)registers it; the runner reports each one.assert.strictEqualis the default comparison.- Run it with
node --testor by running the file.
Keywords and builtins used here
fromfunctionimportreturn
The run, in numbers
- Lines
- 18
- Characters to type
- 438
- Tokens
- 108
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 65 seconds.
Step 1 of 2 in Writing tests, step 1 of 9 in Testing with node:test.