testing.ts

Shared test assertions for the @fuzdev ecosystem.

Extends the fuz-stack testing conventions (assert from vitest, tests in src/test/, plain object mocks) with reusable helpers for patterns that appear across multiple repos. Only depends on vitest — safe for fuz_util's zero-runtime-deps constraint.

Declarations
#

4 declarations

view source

assert_property
#

testing.ts view source

<R extends object, P extends keyof R, const V extends R[P]>(obj: R, property: P, value: V): asserts obj is Extract<R, Record<P, V>>

Narrows a discriminated union by a literal property value, failing the test if the value doesn't match. The assertion signature propagates the narrowed type so callers can access variant-specific fields directly.

Works with any discriminator key (kind, ok, type, _tag, etc.).

obj

type R

property

type P

value

type V

returns

void

generics

R

constraint object

P

constraint keyof R

V

constraint R[P]

examples

type Shape = | {kind: 'circle'; radius: number} | {kind: 'square'; side: number}; const shape: Shape = get_shape(); assert_property(shape, 'kind', 'circle'); assert.strictEqual(shape.radius, 5); // `radius` now typed as `number`

assert_rejects
#

testing.ts view source

(fn: () => Promise<unknown>, pattern?: RegExp | undefined): Promise<Error>

Asserts that fn rejects with an Error. Optionally matches the error message against pattern. Returns the caught Error for further assertions by the caller.

assert.fail is placed after the catch block so that assertion failures from the test itself are not swallowed by the catch.

fn

async function expected to reject

type () => Promise<unknown>

pattern?

optional regex to match against the error message

type RegExp | undefined
optional

returns

Promise<Error>

the caught Error

create_mock_logger
#

testing.ts view source

(): MockLogger

Creates a mock Logger with vi.fn() on each logging method and tracking arrays for inspecting logged messages. Follows the fuz-stack convention of plain object mocks over mocking libraries.

returns

MockLogger

a MockLogger assignable to Logger

MockLogger
#

testing.ts view source

MockLogger

A mock Logger with vi.fn() methods and call tracking arrays. Assignable to Logger for use in code under test. Each tracking array captures the first argument of each call. For full call details, use vi.fn() introspection on the methods directly.