Type-Safe Table-Driven Test Cases

Define named, typed test cases with duplicate-name checks and run them against an executor, returning pass counts and failures.

TypescriptData Integrity

Code

typescriptsnippet
59 lines
export interface TestCase<Input, Expected> {
  name: string;
  input: Input;
  expected: Expected;
}

export interface CaseFailure<Expected> {
  name: string;
  actual: Expected;
  expected: Expected;
}

export interface CaseRunResult<Expected> {
  passed: number;
  failures: Array<CaseFailure<Expected>>;
}

export function defineCases<Input, Expected>(
  cases: ReadonlyArray<TestCase<Input, Expected>>,
): ReadonlyArray<TestCase<Input, Expected>> {
  if (!Array.isArray(cases) || cases.length === 0) {
    throw new RangeError("cases must be a non-empty array");
  }
  const names = new Set<string>();
  for (const testCase of cases) {
    if (typeof testCase.name !== "string" || testCase.name.length === 0) {
      throw new TypeError("each case needs a non-empty name");
    }
    if (names.has(testCase.name)) {
      throw new RangeError("duplicate case name: " + testCase.name);
    }
    names.add(testCase.name);
  }
  return cases;
}

export function runCases<Input, Expected>(
  cases: ReadonlyArray<TestCase<Input, Expected>>,
  execute: (input: Input) => Expected,
  equals: (actual: Expected, expected: Expected) => boolean = Object.is,
): CaseRunResult<Expected> {
  if (typeof execute !== "function") throw new TypeError("execute must be a function");
  if (typeof equals !== "function") throw new TypeError("equals must be a function");
  let passed = 0;
  const failures: Array<CaseFailure<Expected>> = [];
  for (const testCase of cases) {
    const actual = execute(testCase.input);
    if (equals(actual, testCase.expected)) {
      passed += 1;
    } else {
      failures.push({
        name: testCase.name,
        actual,
        expected: testCase.expected,
      });
    }
  }
  return { passed, failures };
}

What the code does

Count the functions defineCases and runCases as the entire surface area of Type-Safe Table-Driven Test Cases.

Invalid states are rejected up front: it throws RangeError and TypeError instead of continuing with bad data.

The result comes back as an object carrying passed.

What to watch for

  • The guard typeof testCase.name !== "string" || testCase.name.length === 0 short-circuits the flow with an early throw when its condition holds.

Using it

defineCases is what another module would consume; nothing else here is load-bearing for callers.

The call returns passed.

Key statements

if (typeof execute !== "function") throw new TypeError("execute must be a function");
if (typeof equals !== "function") throw new TypeError("equals must be a function");
throw new RangeError("duplicate case name: " + testCase.name);

Interactive Tools for This Topic

FAQ

Does `defineCases` modify its input?
No mutation happens — the function reads its input and returns a new value.
What does the snippet do with malformed data?
The code throws `RangeError` and `TypeError` — the guard `typeof testCase.name !== "string" || testCase.name.length…` rejects bad states before the main logic runs. Handle it at the call site or let it propagate deliberately.
Does this work in a bare project with no dependencies file?
Yes — a bare project runs it as-is, since nothing needs declaring. The file defines `defineCases`, `runCases` at load time.