API reference@evolu/common › Assert

Platform-agnostic assertions for invariants, examples, and tests.

An assertion documents a condition required for the program to be correct. Ideally, the type system should enforce it so incorrect code cannot compile, but that is not always possible (not even in Rust). A runtime assertion failure therefore indicates a bug that must be fixed. Throwing at the point of violation prevents invalid state from propagating and makes the defect easier to diagnose.

Evolu provides its own assertions so the same concise API works for production invariants, executable examples, and tests on every platform. The general assert requires a message explaining the invariant, and specialized assertions provide focused diagnostics and narrowing. The API deliberately favors strict and predictable contracts: one assertEqual covers primitives and structural Data comparisons, assertEqualBytes compares bytes across array representations, and assertSame is reserved for SameValue or reference identity. Generic partial structural assertions such as assertMatches are omitted so unmentioned state cannot hide regressions. If a complete expected value is too large to keep inline, use a fixture or a focused domain helper that keeps the contract explicit. assertOk and assertErr provide convenient Result narrowing, while assertType provides runtime Type assertions and compile-time type equality similar to Vitest's expectTypeOf without requiring a test runner. assertThrows and assertRejects verify thrown and rejected values without matcher semantics. Together, these assertions keep documentation examples concise and directly copyable.

In Node.js, failures use the native AssertionError for structured diagnostics and diffs. Other platforms use a compatible fallback. You should not need to import Node.js assertions unless Evolu does not provide an equivalent or a test requires exact Node.js semantics.

Do not use assertions to validate external input. Use a Type declaration's fromUnknown so invalid input is represented by a typed Result.

TODO(next major): Add a separate production build that replaces full assertion messages with numeric error codes and a decoder, following React's approach, to reduce the core bundle size since Evolu has many assertions.

Assertions

NameDescription
assertAsserts that a condition is truthy.
assertFalseAsserts that a value is exactly false and narrows it to false.
assertInstanceOfAsserts that a value is an instance of a constructor and narrows it.
assertLengthAsserts that a value has the expected length and narrows its length.
assertNonEmptyArrayAsserts that an array is non-empty.
assertNonEmptyReadonlyArrayAsserts that a readonly array is non-empty.
assertNonNullableAsserts that a value is non-nullable.
assertNotNullAsserts that a value is not null while preserving undefined.
assertNotUndefinedAsserts that a value is not undefined while preserving null.
assertSameAsserts that two values are the same using Object.is.
assertConditionAfterMicrotasksAsserts that a condition becomes true after exactly the specified number of microtasks.
assertContinuationAfterMicrotasksAsserts that a Promise continuation runs after exactly the specified number of microtasks.
assertEqualAsserts that two values are equal.
assertEqualBytesAsserts that a Uint8Array contains the expected bytes.
assertErrAsserts that a Result is an Err and narrows it, optionally comparing its error.
assertNotDisposedGuards synchronous methods on objects that may be called after disposal.
assertNotEqualAsserts that two values are not equal.
assertNotSameAsserts that two values are not the same using Object.is.
assertOkAsserts that a Result is an Ok and narrows it, optionally comparing its value.
assertRejectsAsserts that a promise rejects with the expected value.
assertRejectsInstanceOfAsserts that a promise rejects with an instance of a constructor.
assertRejectsSameAsserts that a promise rejects with the same value using Object.is.
assertThrowsAsserts that a function throws the expected value.
assertThrowsInstanceOfAsserts that a function throws an instance of a constructor.
assertThrowsSameAsserts that a function throws the same value using Object.is.
assertTrueAsserts that a value is exactly true.