Skip to content

Assert Overview

The assert package provides runtime assertion functions that work in any JavaScript runtime. The API is compatible with Node.js node:assert, so you can write assertions that run identically in Node.js, Deno, Bun, Cloudflare Workers, and browsers.

When to Use This Package

  • Writing tests with the test package
  • Adding runtime invariant checks to application code
  • Validating preconditions in functions that should fail fast on bad input

Quick Example

ts
import { assert, equal, deepEqual, throws } from 'remix/assert'

// Truthy assertion
assert(user != null, 'User must exist')

// Equality
equal(status, 200)
deepEqual(result, { name: 'Alice', age: 30 })

// Exception testing
throws(() => {
  JSON.parse('not json')
}, SyntaxError)

Available Assertions

FunctionWhat It Checks
assert(value, message?)Value is truthy
ok(value, message?)Alias for assert
equal(actual, expected)Loose equality (==)
notEqual(actual, expected)Loose inequality (!=)
strictEqual(actual, expected)Strict equality (===)
notStrictEqual(actual, expected)Strict inequality (!==)
deepEqual(actual, expected)Deep structural equality
notDeepEqual(actual, expected)Deep structural inequality
throws(fn, expected?)Function throws an error
doesNotThrow(fn)Function does not throw
rejects(fn, expected?)Async function rejects
doesNotReject(fn)Async function does not reject
match(string, regexp)String matches a regular expression
doesNotMatch(string, regexp)String does not match
fail(message?)Always throws -- marks unreachable code

Key Concepts

  • AssertionError -- All assertion failures throw an AssertionError with a descriptive message. Test runners catch this to report failures.
  • Cross-runtime -- Unlike node:assert, this package works in every JavaScript environment without polyfills.
  • Compatible API -- If you already know node:assert, you know this package. The function signatures are the same.
  • test -- Test runner that uses these assertions via t.assert
  • API Reference -- Full API documentation

Released under the MIT License.