typestar

describe and it in JavaScript

Nesting groups the output and shares setup between related cases.

import { describe, it } from "node:test";
import assert from "node:assert/strict";

function stars(tpm, target) {
  if (tpm >= target) return 3;
  if (tpm >= target * 0.8) return 2;
  return 1;
}

describe("stars", () => {
  describe("at or above the target", () => {
    it("gives three", () => assert.equal(stars(100, 100), 3));
    it("gives three when faster", () => assert.equal(stars(140, 100), 3));
  });

  describe("below the target", () => {
    it("gives two when close", () => assert.equal(stars(85, 100), 2));
    it("gives one when slow", () => assert.equal(stars(40, 100), 1));
  });
});

How it works

  1. describe groups; it is the same as test.
  2. Nested describes read as sentences in the report.
  3. A failure names the whole path, so it is easy to find.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
572
Tokens
167
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 100 seconds.

Type this snippet

Step 2 of 2 in Writing tests, step 2 of 9 in Testing with node:test.

← Previous Next →