typestar

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

  1. assert/strict makes every comparison strict.
  2. deepStrictEqual compares structure and types.
  3. assert.throws takes a matcher object or a regex.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 2 in Selecting, step 8 of 9 in Testing with node:test.

← Previous Next →