The assert module in JavaScript
Deep equality, throws, and the strict variants you should prefer.
import test from "node:test";
import assert from "node:assert/strict";
test("deep comparisons", () => {
assert.deepStrictEqual({ a: [1, 2] }, { a: [1, 2] });
assert.notDeepStrictEqual({ a: 1 }, { a: "1" });
assert.deepStrictEqual(new Set([1]), new Set([1]));
});
test("throwing", () => {
assert.throws(() => JSON.parse("{oops"), SyntaxError);
assert.throws(() => { throw new TypeError("bad shape"); }, /shape/);
assert.doesNotThrow(() => JSON.parse("{}"));
});
test("numbers and truthiness", () => {
assert.ok(0.1 + 0.2 !== 0.3);
assert.match("typestar.io", /^[a-z]+\.io$/);
});
How it works
assert/strictmakes every comparison strict.deepStrictEqualcompares structure and types.assert.throwstakes a matcher object or a regex.
Keywords and builtins used here
JSONSetfromimportthrowthrows
The run, in numbers
- Lines
- 19
- Characters to type
- 581
- Tokens
- 178
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 102 seconds.
Step 2 of 2 in Selecting, step 8 of 9 in Testing with node:test.