A comprehensive assertion library for JavaScript and TypeScript, providing type-safe and expressive assertions for testing and validation.
- π§ͺ Type-safe assertions with automatic type narrowing in TypeScript
- π Comprehensive matchers for primitives, objects, arrays, maps, sets, and more
- π Async support with
assertResolvesandassertRejectsutilities - π§© Combinators like
assertAnyOfandassertNoneOffor complex conditions - π Structured errors with detailed diagnostic information for debugging
-
Deno:
deno add @dep/assert
-
Node.js (18+) or Browsers:
npx jsr add @dep/assert
Then import as an ES module:
import { assertArray, assertEqual, assertTruthy } from "@dep/assert";
import {
assertAnyOf,
assertArray,
assertContain,
assertDeepEqual,
assertEqual,
assertGreaterThan,
assertRejects,
assertResolves,
assertString,
assertTruthy,
} from "@dep/assert";
// Basic type assertions
assertString("hello"); // Type narrowed to string
assertArray([1, 2, 3]); // Type narrowed to Array<unknown>
// Equality checks
assertEqual(42, 42);
assertDeepEqual({ a: 1, b: [2, 3] }, { a: 1, b: [2, 3] });
// Numeric comparisons
assertGreaterThan(10, 5);
assertLessThanOrEqual(5, 10);
// Collection checks
assertContain([1, 2, 3], 2);
assertContain("hello world", "world");
assertContain(new Set([1, 2, 3]), 2);
// Length assertions
assertLength([1, 2, 3], 3);
assertLength("hello", 5);
assertLength(new Map([["a", 1]]), 1);
// Async assertions
await assertResolves(async () => {
return await fetchData();
});
await assertRejects(async () => {
throw new Error("Failed");
});
// Combinators
const value: unknown = getValue();
assertAnyOf(
() => assertString(value),
() => assertNumber(value),
() => assertArray(value),
);
// Proximity checks
assertCloseTo(3.14159, 3.14, 0.01);
// Instance checks
assertInstanceOf(new Date(), Date);
assertDate(new Date());assertEqual(getUser().age, 25, "User should be 25 years old");
assertContain(getUser().roles, "admin", "User should have admin role");import { AssertionError } from "@dep/assert";
try {
assertEqual(1, 2);
} catch (error) {
if (error instanceof AssertionError) {
console.error(error.code); // 'NOT_EQUAL'
console.error(error.message); // 'Expected 1, but got 2'
console.error(error.expected); // 2
console.error(error.received); // 1
}
}assertArray- Checks if value is an arrayassertBigint- Checks if value is a bigintassertBoolean- Checks if value is a booleanassertDate- Checks if value is a DateassertFunction- Checks if value is a functionassertMap- Checks if value is a MapassertNumber- Checks if value is a numberassertObject- Checks if value is a plain objectassertRecord- Checks if value is a recordassertRegExp- Checks if value is a RegExpassertSet- Checks if value is a SetassertString- Checks if value is a stringassertSymbol- Checks if value is a symbolassertThenable- Checks if value is Promise-like
assertDefined- Checks if value is not undefinedassertFalsy- Checks if value is falsyassertNaN- Checks if value is NaNassertNull- Checks if value is nullassertTruthy- Checks if value is truthyassertUndefined- Checks if value is undefined
assertEqual- Strict equality (Object.is)assertDeepEqual- Deep equality for objects and arraysassertCloseTo- Numeric proximity within toleranceassertGreaterThan- Greater than comparisonassertGreaterThanOrEqual- Greater than or equal comparisonassertLessThan- Less than comparisonassertLessThanOrEqual- Less than or equal comparison
assertContain- Checks if value contains element/keyassertLength- Checks exact length/sizeassertMatch- String matches regular expressionassertInstanceOf- Value is instance of constructor
assertAnyOf- At least one assertion passesassertNoneOf- No assertions passassertRejects- Async function rejectsassertResolves- Async function resolvesassertThrow- Synchronous function throws
The library fully supports TypeScript's type narrowing:
function processValue(value: unknown) {
assertString(value); // Type is now narrowed to string
console.log(value.toUpperCase()); // Safe!
const data: unknown = getData();
assertArray<number>(data); // Type is now Array<number>
data.forEach((num) => console.log(num * 2)); // Safe!
}MIT License β see LICENSE for details.
Author: Estarlin R (estarlincito.com)