Skip to main content
Home
This release is 18 versions behind 1.0.16 — the latest version of @std/assert. Jump to latest

@std/assert@0.225.3
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%
Downloads61,719/wk
Published2 years ago (0.225.3)

Common assertion functions, especially useful for testing

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. import { AssertionError } from "./assertion_error.ts"; /** * Make an assertion that actual is not null or undefined. * If not then throw. * * @example * ```ts * import { assertExists } from "@std/assert/assert-exists"; * * assertExists("something"); // Doesn't throw * assertExists(undefined); // Throws * ``` */ export function assertExists<T>( actual: T, msg?: string, ): asserts actual is NonNullable<T> { if (actual === undefined || actual === null) { const msgSuffix = msg ? `: ${msg}` : "."; msg = `Expected actual: "${actual}" to not be null or undefined${msgSuffix}`; throw new AssertionError(msg); } }