export function assert(value: unknown, message = "Assertion failed"): asserts value { if (!value) throw new Error(message); } export function equal(actual: unknown, expected: unknown): void { const canonical = (value: unknown): string | undefined => JSON.stringify( value, (_, item) => item !== null && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) : item, ); assert( canonical(actual) === canonical(expected), `Expected:\n${Deno.inspect(expected, { depth: Infinity })}\nReceived:\n${ Deno.inspect(actual, { depth: Infinity }) }`, ); } export function throws(fn: () => unknown, message: string): void { try { fn(); } catch (error) { assert(error instanceof Error && error.message.includes(message), `Unexpected error: ${error}`); return; } throw new Error(`Expected an error containing ${JSON.stringify(message)}`); }