@jakobkg/expect
    Preparing search index...

    @jakobkg/expect

    Tiny-ish, type-aware test utility, providing expect() which wraps Node's built-in asserts.

    • Node.js 20 or newer, for node:assert
    • ES modules only, there is no CommonJS build.

    This package is published on Codeberg's package registry, so consuming it requires a .npmrc file with the following line:

    @jakobkg:registry=https://codeberg.org/api/packages/jakobkg/npm/
    

    With this in place, the package can be installed as usual with npm install -D @jakobkg/expect.

    The provided expect() function provides a Jest-like API for assertions in tests, but with Chai-like chainability. For example, the following is a perfectly fine usage of this package:

    import { expect } from "@jakobkg/expect";

    const arr = ["a", "b", "c"];

    expect(arr).toHaveLength(3).and.not.toContain("unwanted element");

    By the magic of writing a few truly horrific type definitions, expect() changes what matchers are available based on the type of its input and gives a type error if a test tries to do something impossible.

    For example, this is fine:

    expect("stringy string").not.toBeNull().and.toHaveLength(14);
    

    But this is not:

    expect(12).toHaveLength(2); // <- Property 'toHaveLength' does not exist on type 'Expect<number>'
    

    These checks come from TypeScript, but the package works just as well from plain JavaScript. The assertions behave the same, there's just no compile-time safety net.

    A sample test file could look like:

    import { test } from "node:test";
    import { expect } from "@jakobkg/expect";

    test("first-grade maths", () => {
    expect(1 + 2)
    .toBe(3)
    .and.toBeLessThan(10);
    });

    Failures are reported as plain AssertionErrors, so any test runner that handles these like Node's node --test, Mocha and probably others can be used.

    Matchers are chosen from the type of the value passed to expect(). Type-specific matchers only appear when the whole type fits one of the supported kinds, so a union such as string | undefined only gets the common matchers until it is narrowed. This also applies to unions whose members would each qualify on their own: string | string[] gets no toHaveLength. toBePresent() checks for null/undefined at runtime, but it can't narrow the type for later lines:

    const name: string | undefined = getName();

    expect(name).toHaveLength(4); // <- type error: not every member of the union type has a length

    expect(name).toBePresent();
    expect(name!).toHaveLength(4); // fine: `!` narrows to `string`

    If I were a smarter man .toBePresent() would narrow types when chained, but alas.

    For a value typed any, TypeScript can't tell which matchers should be available so all of them are offered. At runtime only the matchers that fit the actual value exist, so calling one that doesn't throws a plain TypeError (expect(...).toHaveLength is not a function or similar) rather than an AssertionError which would cause tests to fail with less helpful output. Giving the value a real type restores the type checking.