Skip to main content

@std/assert@0.215.0

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Published2 years ago (0.215.0)
default

A library of assertion functions. If the assertion is false an AssertionError will be thrown which will result in pretty-printed diff of failing assertion.

Classes

c
AssertionError(message: string)

Error thrown when an assertion fails.

Functions

f
assert(
expr: unknown,
msg?
): asserts expr

Make an assertion, error will be thrown if expr does not have truthy value.

f
assertAlmostEquals(
actual: number,
expected: number,
tolerance?,
msg?: string
): void

Make an assertion that actual and expected are almost equal numbers through a given tolerance. It can be used to take into account IEEE-754 double-precision floating-point representation limitations. If the values are not almost equal then throw.

f
assertArrayIncludes<T>(
actual: ArrayLikeArg<T>,
expected: ArrayLikeArg<T>,
msg?: string
): void

Make an assertion that actual includes the expected values. If not then an error will be thrown.

f
assertEquals<T>(
actual: T,
expected: T,
msg?: string,
options?: { formatter?: (value: unknown) => string; }
): void

Make an assertion that actual and expected are equal, deeply. If not deeply equal, then throw.

f
assertExists<T>(
actual: T,
msg?: string
): asserts actual is NonNullable<T>

Make an assertion that actual is not null or undefined. If not then throw.

f
assertFalse(
expr: unknown,
msg?
): asserts expr is Falsy

Make an assertion, error will be thrown if expr have truthy value.

f
assertGreater<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is greater than expected. If not then throw.

f
assertGreaterOrEqual<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is greater than or equal to expected. If not then throw.

f
assertInstanceOf<T extends AnyConstructor>(
actual: unknown,
expectedType: T,
msg?
): asserts actual is GetConstructorType<T>

Make an assertion that obj is an instance of type. If not then throw.

f
assertIsError<E extends Error = Error>(
error: unknown,
ErrorClass?: new (...args: any[]) => E,
msgMatches?: string | RegExp,
msg?: string
): asserts error is E

Make an assertion that error is an Error. If not then an error will be thrown. An error class and a string that should be included in the error message can also be asserted.

f
assertLess<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is less than expected. If not then throw.

f
assertLessOrEqual<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is less than or equal to expected. If not then throw.

f
assertMatch(
actual: string,
expected: RegExp,
msg?: string
): void

Make an assertion that actual match RegExp expected. If not then throw.

f
assertNotEquals<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual and expected are not equal, deeply. If not then throw.

f
assertNotInstanceOf<A, T>(
actual: A,
unexpectedType: new (...args: any[]) => T,
msg?: string
): asserts actual is Exclude<A, T>

Make an assertion that obj is not an instance of type. If so, then throw.

f
assertNotMatch(
actual: string,
expected: RegExp,
msg?: string
): void

Make an assertion that actual not match RegExp expected. If match then throw.

f
assertNotStrictEquals<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual and expected are not strictly equal. If the values are strictly equal then throw.

f
assertObjectMatch(
actual: Record<PropertyKey, any>,
expected: Record<PropertyKey, unknown>,
msg?: string
): void

Make an assertion that actual object is a subset of expected object, deeply. If not, then throw.

f
assertRejects<E extends Error = Error>(
fn: () => PromiseLike<unknown>,
errorClassOrMsg?: (new (...args: any[]) => E) | string,
msgIncludesOrMsg?: string,
msg?: string
): Promise<E | Error | unknown>
2 overloads

Executes a function which returns a promise, expecting it to reject.

f
assertStrictEquals<T>(
actual: unknown,
expected: T,
msg?: string
): asserts actual is T

Make an assertion that actual and expected are strictly equal. If not then throw.

f
assertStringIncludes(
actual: string,
expected: string,
msg?: string
): void

Make an assertion that actual includes expected. If not then throw.

f
assertThrows<E extends Error = Error>(
fn: () => unknown,
errorClassOrMsg?: (new (...args: any[]) => E) | string,
msgIncludesOrMsg?: string,
msg?: string
): E | Error | unknown
2 overloads

Executes a function, expecting it to throw. If it does not, then it throws.

f
equal(
c: unknown,
d: unknown
): boolean

Deep equality comparison used in assertions

f
fail(msg?: string): never

Forcefully throws a failed assertion.

f
unimplemented(msg?: string): never

Use this to stub out methods that will throw when invoked.

f
unreachable(): never

Use this to assert unreachable code.

Type Aliases

T
AnyConstructor = new (...args: any[]) => any

Any constructor

T
ArrayLikeArg<T> = ArrayLike<T> & object

An array-like object (Array, Uint8Array, NodeList, etc.) that is not a string

T
Falsy = false | 0 | 0 | "" | null | undefined

Assertion condition for assertFalse.

T
GetConstructorType<T extends AnyConstructor> = T extends new (...args: any) => infer C ? C : never

Gets constructor type

assert

Functions

f
assert(
expr: unknown,
msg?
): asserts expr

Make an assertion, error will be thrown if expr does not have truthy value.

assert_almost_equals

Functions

f
assertAlmostEquals(
actual: number,
expected: number,
tolerance?,
msg?: string
): void

Make an assertion that actual and expected are almost equal numbers through a given tolerance. It can be used to take into account IEEE-754 double-precision floating-point representation limitations. If the values are not almost equal then throw.

assert_array_includes

Functions

f
assertArrayIncludes<T>(
actual: ArrayLikeArg<T>,
expected: ArrayLikeArg<T>,
msg?: string
): void

Make an assertion that actual includes the expected values. If not then an error will be thrown.

Type Aliases

T
ArrayLikeArg<T> = ArrayLike<T> & object

An array-like object (Array, Uint8Array, NodeList, etc.) that is not a string

assert_equals

Functions

f
assertEquals<T>(
actual: T,
expected: T,
msg?: string,
options?: { formatter?: (value: unknown) => string; }
): void

Make an assertion that actual and expected are equal, deeply. If not deeply equal, then throw.

assert_exists

Functions

f
assertExists<T>(
actual: T,
msg?: string
): asserts actual is NonNullable<T>

Make an assertion that actual is not null or undefined. If not then throw.

assert_false

Functions

f
assertFalse(
expr: unknown,
msg?
): asserts expr is Falsy

Make an assertion, error will be thrown if expr have truthy value.

Type Aliases

T
Falsy = false | 0 | 0 | "" | null | undefined

Assertion condition for assertFalse.

assert_greater

Functions

f
assertGreater<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is greater than expected. If not then throw.

assert_greater_or_equal

Functions

f
assertGreaterOrEqual<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is greater than or equal to expected. If not then throw.

assert_instance_of

Functions

f
assertInstanceOf<T extends AnyConstructor>(
actual: unknown,
expectedType: T,
msg?
): asserts actual is GetConstructorType<T>

Make an assertion that obj is an instance of type. If not then throw.

Type Aliases

T
AnyConstructor = new (...args: any[]) => any

Any constructor

T
GetConstructorType<T extends AnyConstructor> = T extends new (...args: any) => infer C ? C : never

Gets constructor type

assert_is_error

Functions

f
assertIsError<E extends Error = Error>(
error: unknown,
ErrorClass?: new (...args: any[]) => E,
msgMatches?: string | RegExp,
msg?: string
): asserts error is E

Make an assertion that error is an Error. If not then an error will be thrown. An error class and a string that should be included in the error message can also be asserted.

assert_less

Functions

f
assertLess<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is less than expected. If not then throw.

assert_less_or_equal

Functions

f
assertLessOrEqual<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual is less than or equal to expected. If not then throw.

assert_match

Functions

f
assertMatch(
actual: string,
expected: RegExp,
msg?: string
): void

Make an assertion that actual match RegExp expected. If not then throw.

assert_not_equals

Functions

f
assertNotEquals<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual and expected are not equal, deeply. If not then throw.

assert_not_instance_of

Functions

f
assertNotInstanceOf<A, T>(
actual: A,
unexpectedType: new (...args: any[]) => T,
msg?: string
): asserts actual is Exclude<A, T>

Make an assertion that obj is not an instance of type. If so, then throw.

assert_not_match

Functions

f
assertNotMatch(
actual: string,
expected: RegExp,
msg?: string
): void

Make an assertion that actual not match RegExp expected. If match then throw.

assert_not_strict_equals

Functions

f
assertNotStrictEquals<T>(
actual: T,
expected: T,
msg?: string
): void

Make an assertion that actual and expected are not strictly equal. If the values are strictly equal then throw.

assert_object_match

Functions

f
assertObjectMatch(
actual: Record<PropertyKey, any>,
expected: Record<PropertyKey, unknown>,
msg?: string
): void

Make an assertion that actual object is a subset of expected object, deeply. If not, then throw.

assert_rejects

Functions

f
assertRejects<E extends Error = Error>(
fn: () => PromiseLike<unknown>,
errorClassOrMsg?: (new (...args: any[]) => E) | string,
msgIncludesOrMsg?: string,
msg?: string
): Promise<E | Error | unknown>
2 overloads

Executes a function which returns a promise, expecting it to reject.

assert_strict_equals

Functions

f
assertStrictEquals<T>(
actual: unknown,
expected: T,
msg?: string
): asserts actual is T

Make an assertion that actual and expected are strictly equal. If not then throw.

assert_string_includes

Functions

f
assertStringIncludes(
actual: string,
expected: string,
msg?: string
): void

Make an assertion that actual includes expected. If not then throw.

assert_throws

Functions

f
assertThrows<E extends Error = Error>(
fn: () => unknown,
errorClassOrMsg?: (new (...args: any[]) => E) | string,
msgIncludesOrMsg?: string,
msg?: string
): E | Error | unknown
2 overloads

Executes a function, expecting it to throw. If it does not, then it throws.

assertion_error

Classes

c
AssertionError(message: string)

Error thrown when an assertion fails.

equal

Functions

f
equal(
c: unknown,
d: unknown
): boolean

Deep equality comparison used in assertions

fail

Functions

f
fail(msg?: string): never

Forcefully throws a failed assertion.

unimplemented

Functions

f
unimplemented(msg?: string): never

Use this to stub out methods that will throw when invoked.

unreachable

Functions

f
unreachable(): never

Use this to assert unreachable code.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/assert

Import symbol

import * as assert from "@std/assert";
or

Import directly with a jsr specifier

import * as assert from "jsr:@std/assert";