Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Testing

Node’s built-in runner for JavaScript/compiled TypeScript, Go testing, Rust cargo test, and optional GoogleTest for C++. Excerpts assume a test body and named application functions unless marked as complete.

Declare a test

TypeScript

Import the built-in runner and strict assertion API. A test fails when its body throws or its returned promise rejects.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import test from "node:test";
import assert from "node:assert/strict";
test("doubles a value", () => {
  assert.equal(double(3), 6);
});

Run the suite with its native runner

TypeScript

Compile the project first when using a compiled TypeScript workflow, then run emitted tests.

Example shell commands: npx tsc -p tsconfig.json; node --test out/example.test.js. Configure NodeNext/ESM consistently with your package.

Name related cases

TypeScript

Await subtests so the parent owns their completion.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
test("cases", async (t) => {
  await t.test("zero", () => assert.equal(double(0), 0));
});

Choose equality with the right semantics

TypeScript

Strict equal compares object identity; deepEqual under node:assert/strict compares supported nested structures.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
assert.equal(actualCount, 3);
assert.deepEqual(actualItems, ["a", "b"]);

Compare floating-point values with a domain tolerance

TypeScript

Choose absolute/relative tolerances from the domain and reject non-finite values when inappropriate.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const tolerance = 1e-9 * Math.max(1, Math.abs(expected));
assert.ok(Number.isFinite(actual));
assert.ok(Math.abs(actual - expected) <= tolerance);

Check expected failures

TypeScript

throws checks a synchronous exception; rejects checks an asynchronous rejection.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
assert.throws(() => parse("bad"), /invalid/);
await assert.rejects(() => loadMissing(), /missing/);

Report observed values and the case identity

TypeScript

A useful failure says which input failed, what was observed and what was expected. Assert public behavior; avoid pinning incidental private details that are free to change.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
case: empty input
observed: one item
expected: no items

Register cleanup with ownership

TypeScript

t.after runs after the test. Await asynchronous cleanup and register it as soon as the resource is acquired.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
test("resource", async (t) => {
  const resource = await openResource();
  t.after(async () => { await resource.close(); });
  assert.ok(resource.ready);
});

Wait for the actual asynchronous result

TypeScript

Return/await the promise. Unawaited work can escape the test’s failure and cleanup lifetime.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
test("loads", async () => {
  const value = await load();
  assert.equal(value, "ready");
});

Bound a wait and stop owned work

TypeScript

A test timeout reports overrun; signal-aware owned work must also cooperate with cancellation.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
test("loads", { timeout: 1000 }, async (t) => {
  await load({ signal: t.signal });
});

Give each test its own mutable resources

TypeScript

Use temporary directories, unique database names/keys, random free ports and explicit clocks/configuration. Restore global state in cleanup when isolation is impossible. Tests that run in parallel must not mutate the same process-wide state unsafely.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
test A → its own directory / namespace
test B → its own directory / namespace

Choose a double for a boundary

TypeScript

A stub supplies answers, a fake implements simplified behavior and a mock verifies chosen interactions. Keep essential protocol semantics in the double; separately test the real adapter against the real dependency.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
core logic → fake clock/storage
adapter test → real clock/storage boundary

Control time without arbitrary sleeps

TypeScript

Inject a clock/scheduler or use the runner’s documented fake-time facilities. Advance to an observable condition; sleeping a guessed duration creates slow and flaky tests. Real I/O and other threads may not use the fake clock.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
arrange a deadline
advance the controlled clock
assert the observable transition

Pick boundary and equivalence cases

TypeScript

Cover ordinary input, empty/min/max values, malformed input, duplicates, missing values and meaningful ordering. Derive cases from the contract rather than repeating the implementation’s branches.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
empty / one / many
just below / at / just above a threshold
valid / malformed / missing

State an invariant before generating inputs

TypeScript

Property testing checks a relationship over generated inputs and often shrinks a failure. The language’s test runner alone does not always supply a property framework. Define input constraints and preserve the failing seed/example.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
decode(encode(value)) = value
sort(sort(values)) = sort(values)
output length = input length

Use an explicit fuzzing tool

TypeScript

Node’s test runner has no built-in general property generator/shrinker. An optional library such as fast-check can supply one.

Fragment

Optional fast-check import; choose a bounded numeric domain that matches the actual function contract.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
fc.assert(fc.property(fc.integer(), (n) => {
  assert.equal(double(n), n * 2);
}));

Test the real boundary with a focused integration case

TypeScript

Start the actual dependency with a known configuration, create isolated data, exercise one external contract and tear it down. Include serialization, error mapping, timeouts and authorization where they belong to that boundary.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
unit: business rule with fake storage
integration: real driver + disposable database
end-to-end: supported user workflow

Use coverage to find unexercised paths

TypeScript

Coverage shows what ran under a selected instrumentation model. A high percentage does not prove correct assertions, all input combinations or absence of races. Inspect missing meaningful behavior rather than forcing a target number.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
line / branch / function coverage
covered code can still have a wrong assertion