Skip to content

pinned-constant-assert ​

A test that asserts a constant has the literal value it is declared with: expect(ENCODING_VERSION).toBe(34), assert.equal(Op.Hello, 1). Reported in test files (named *.test.ts / *.spec.ts, importing a test runner or node:assert, or in a test, tests or __tests__ directory). A warning by default.

Why: the test restates the declaration, so it can't catch a bug. It fails on every intended change of the constant. It also fails when two branches that each bumped the constant are merged: the code is right, and only the copy in the test is stale. An adopter's main branch broke twice this way after merges, on ENCODING_VERSION === 34. Guide: Testing.

ts
import assert from "node:assert/strict";
import { test } from "node:test";

// Usually imported: `import { ENCODING_VERSION, encode } from "../src/layout";`
const ENCODING_VERSION = 34;

function encode(units: readonly number[]): string {
  return `${ENCODING_VERSION}:${units.join(",")}`;
}

test("the encoding version", () => {
  assert.equal(ENCODING_VERSION, 34);
  assert.equal(encode([7]), "34:7");
});

✅ Accepted: test what the constant is for

✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";

const ENCODING_VERSION = 34;
const LEGACY_VERSIONS = [32, 33] as const;

function encode(units: readonly number[]): string {
  return `${ENCODING_VERSION}:${units.join(",")}`;
}

test("a snapshot names the version it was written with", () => {
  assert.equal(encode([7]), `${ENCODING_VERSION}:7`);
  assert.ok(LEGACY_VERSIONS.every((v) => v < ENCODING_VERSION));
});

What counts ​

  • Assertions of equality: expect(X).toBe(lit), .toEqual, .toStrictEqual (vitest, jest, bun, also expect.soft and a global expect), chai's expect(X).to.equal(lit), assert.equal, assert.strictEqual, assert.deepEqual, assert.deepStrictEqual (node, chai, also imported by name: strictEqual(X, lit)), and assert(X === lit) / assert.ok(X === lit). Either way round.
  • The constant: an imported binding or a module-level const of the test file, or a member of one (Op.Hello), whose value is a literal: const X = 34, a member of an as const or frozen table, a code built with codesOf. Its value is read across files, the way the JSON guide reads typeof X.
  • A pin on purpose says why in the assertion's message, and is left alone: a wire code old clients still send, a value stored on disk.

✅ Accepted: a pin with its reason

✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";

const Op = { Hello: 1, Bye: 2 } as const;

test("connection codes are frozen", () => {
  assert.equal(Op.Hello, 1, "HELLO is frozen: clients of every version send it first");
});
  • Left alone: a negated assertion (.not.toBe(33)), comparisons (toBeGreaterThan), a constant compared with another value the test computes, and a const typed by an annotation (const MAX: number = 500), whose type isn't the literal.

What the rule can't see ​

A constant whose value is computed (const VERSION = hash(TABLES)) has no literal to restate, so a test of it is a real test. An assertion through a helper of the test's own (expectVersion(34)) isn't recognised.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra