Skip to content

test-only-helper ​

A test helper from @evobug_com/safe-ts-std/testing (fixture, testDouble, unchecked, expectPresent) used outside a test file.

Why ​

Each of them says a value has a type without checking it, or stops when it is absent:

  • fixture<T>(fields) is a T from the fields a test needs; the rest are missing;
  • testDouble<T>(fields) is a fake T that throws when code reads a field it wasn't given;
  • unchecked<T>(value) is any value typed T, for feeding code the wrong type on purpose;
  • expectPresent(value) is the value, or a failed test when it is undefined or null.

In a test that is what you want: the test fails loudly when the value is wrong, and you don't write out 50 fields of an object to test one. Anywhere else each is a cast (no-unchecked-cast), so they are allowed only in test files: files named *.test.* or *.spec.*, files in a test, tests, __tests__ or __mocks__ directory of their package, and files that import a test runner (node:test, vitest). Their names make them easy to find. Only the directories below the nearest package.json count: a checkout in /home/ci/tests/app isn't all test files, and neither is a workspace package named tests (its src/ isn't a test directory; its test/ is). Outside any package, only the file's own directory counts.

Examples ​

✕ Rejectedtest-only-helper
ts
import { fixture } from "@evobug_com/safe-ts-std/testing";

type Order = { readonly id: string; readonly total: number; readonly items: readonly string[] };

// Not a test: the order has no `id` or `items`, whatever its type says.
export const sampleOrder = fixture<Order>({ total: 12 });
✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";

import { expectPresent, fixture, testDouble } from "@evobug_com/safe-ts-std/testing";

type Customer = { readonly name: string; readonly country: string; readonly vip: boolean };
type Order = { readonly id: string; readonly total: number; readonly customer: Customer };
type Mailer = { readonly send: (to: string, text: string) => string; readonly unsubscribe: (to: string) => void };

function shipping(order: Order): number {
  return order.customer.country === "CZ" ? 5 : 15;
}

function confirm(mailer: Mailer, order: Order): string {
  return mailer.send(order.customer.name, `Order ${order.id}: ${order.total}`);
}

test("shipping depends only on the country", () => {
  assert.equal(shipping(fixture<Order>({ customer: { country: "CZ" } })), 5);
});

test("a confirmation goes to the customer", () => {
  const sent: string[] = [];
  const mailer = testDouble<Mailer>({ send: (to) => expectPresent(sent.push(to) > 0 ? to : undefined) });
  assert.equal(confirm(mailer, fixture<Order>({ id: "7", total: 12, customer: { name: "Ada" } })), "Ada");
});

The testing guide shows each helper.

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