close
Skip to main content

@std/assert@1.0.19
Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
License
MIT
Downloads5,090/wk
Published2 months ago (1.0.19)

Common assertion functions, especially useful for testing

Classes

c
AssertionError(
message: string,
options?: ErrorOptions
)

Error thrown when an assertion fails.

Functions

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

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

f
assertAlmostEquals(
actual: number,
expected: number,
tolerance?: number,
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

Asserts that actual contains all values in expected, using deep equality for non-primitive values.

f
assertEquals<T>(
actual: T,
expected: T,
msg?: 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?: string
): asserts expr is Falsy

Make an assertion, an error will be thrown if expr has a 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 new (...args: any[]) => any>(
actual: unknown,
expectedType: T,
msg?: string
): asserts actual is InstanceType<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, using Object.is for equality comparison. 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 expected object is a subset of actual object, deeply. If not, then throw a diff of the objects, with mismatching properties highlighted.

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, using Object.is for equality comparison. 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(
a: unknown,
b: 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(msg?: string): 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> = InstanceType<T>

Gets constructor type

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.