[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/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](https://evolu.dev/docs/api-reference/common/Assert/variables/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](https://evolu.dev/docs/api-reference/common/Assert/functions/assertEqual)
covers primitives and structural Data comparisons, [assertEqualBytes](https://evolu.dev/docs/api-reference/common/Assert/functions/assertEqualBytes)
compares bytes across array representations, and [assertSame](https://evolu.dev/docs/api-reference/common/Assert/variables/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](https://evolu.dev/docs/api-reference/common/Assert/functions/assertOk) and [assertErr](https://evolu.dev/docs/api-reference/common/Assert/functions/assertErr) provide convenient Result narrowing,
while [assertType](https://evolu.dev/docs/api-reference/common/Type/functions/assertType) provides runtime Type assertions and compile-time
type equality similar to Vitest's `expectTypeOf` without requiring a test
runner. [assertThrows](https://evolu.dev/docs/api-reference/common/Assert/functions/assertThrows) and [assertRejects](https://evolu.dev/docs/api-reference/common/Assert/functions/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](https://evolu.dev/docs/api-reference/common/Type/interfaces/Type)
declaration's `fromUnknown` so invalid input is represented by a typed
[Result](https://evolu.dev/docs/api-reference/common/Result/type-aliases/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

| Name                                                                                                                   | Description                                                                                                                                                                                          |
| ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [assert](https://evolu.dev/docs/api-reference/common/Assert/variables/assert)                                                       | Asserts that a condition is truthy.                                                                                                                                                                  |
| [assertFalse](https://evolu.dev/docs/api-reference/common/Assert/variables/assertFalse)                                             | Asserts that a value is exactly `false` and narrows it to `false`.                                                                                                                                   |
| [assertInstanceOf](https://evolu.dev/docs/api-reference/common/Assert/variables/assertInstanceOf)                                   | Asserts that a value is an instance of a constructor and narrows it.                                                                                                                                 |
| [assertLength](https://evolu.dev/docs/api-reference/common/Assert/variables/assertLength)                                           | Asserts that a value has the expected length and narrows its length.                                                                                                                                 |
| [assertNonEmptyArray](https://evolu.dev/docs/api-reference/common/Assert/variables/assertNonEmptyArray)                             | Asserts that an array is non-empty.                                                                                                                                                                  |
| [assertNonEmptyReadonlyArray](https://evolu.dev/docs/api-reference/common/Assert/variables/assertNonEmptyReadonlyArray)             | Asserts that a readonly array is non-empty.                                                                                                                                                          |
| [assertNonNullable](https://evolu.dev/docs/api-reference/common/Assert/variables/assertNonNullable)                                 | Asserts that a value is non-nullable.                                                                                                                                                                |
| [assertNotNull](https://evolu.dev/docs/api-reference/common/Assert/variables/assertNotNull)                                         | Asserts that a value is not null while preserving undefined.                                                                                                                                         |
| [assertNotUndefined](https://evolu.dev/docs/api-reference/common/Assert/variables/assertNotUndefined)                               | Asserts that a value is not undefined while preserving null.                                                                                                                                         |
| [assertSame](https://evolu.dev/docs/api-reference/common/Assert/variables/assertSame)                                               | Asserts that two values are the same using `Object.is`.                                                                                                                                              |
| [assertConditionAfterMicrotasks](https://evolu.dev/docs/api-reference/common/Assert/functions/assertConditionAfterMicrotasks)       | Asserts that a condition becomes true after exactly the specified number of microtasks.                                                                                                              |
| [assertContinuationAfterMicrotasks](https://evolu.dev/docs/api-reference/common/Assert/functions/assertContinuationAfterMicrotasks) | Asserts that a Promise continuation runs after exactly the specified number of microtasks.                                                                                                           |
| [assertEqual](https://evolu.dev/docs/api-reference/common/Assert/functions/assertEqual)                                             | Asserts that two values are equal.                                                                                                                                                                   |
| [assertEqualBytes](https://evolu.dev/docs/api-reference/common/Assert/functions/assertEqualBytes)                                   | Asserts that a `Uint8Array` contains the expected bytes.                                                                                                                                             |
| [assertErr](https://evolu.dev/docs/api-reference/common/Assert/functions/assertErr)                                                 | Asserts that a [Result](https://evolu.dev/docs/api-reference/common/Result/type-aliases/Result) is an [Err](https://evolu.dev/docs/api-reference/common/Result/interfaces/Err) and narrows it, optionally comparing its error. |
| [assertNotDisposed](https://evolu.dev/docs/api-reference/common/Assert/functions/assertNotDisposed)                                 | Guards synchronous methods on objects that may be called after disposal.                                                                                                                             |
| [assertNotEqual](https://evolu.dev/docs/api-reference/common/Assert/functions/assertNotEqual)                                       | Asserts that two values are not equal.                                                                                                                                                               |
| [assertNotSame](https://evolu.dev/docs/api-reference/common/Assert/functions/assertNotSame)                                         | Asserts that two values are not the same using `Object.is`.                                                                                                                                          |
| [assertOk](https://evolu.dev/docs/api-reference/common/Assert/functions/assertOk)                                                   | Asserts that a [Result](https://evolu.dev/docs/api-reference/common/Result/type-aliases/Result) is an [Ok](https://evolu.dev/docs/api-reference/common/Result/interfaces/Ok) and narrows it, optionally comparing its value.   |
| [assertRejects](https://evolu.dev/docs/api-reference/common/Assert/functions/assertRejects)                                         | Asserts that a promise rejects with the expected value.                                                                                                                                              |
| [assertRejectsInstanceOf](https://evolu.dev/docs/api-reference/common/Assert/functions/assertRejectsInstanceOf)                     | Asserts that a promise rejects with an instance of a constructor.                                                                                                                                    |
| [assertRejectsSame](https://evolu.dev/docs/api-reference/common/Assert/functions/assertRejectsSame)                                 | Asserts that a promise rejects with the same value using `Object.is`.                                                                                                                                |
| [assertThrows](https://evolu.dev/docs/api-reference/common/Assert/functions/assertThrows)                                           | Asserts that a function throws the expected value.                                                                                                                                                   |
| [assertThrowsInstanceOf](https://evolu.dev/docs/api-reference/common/Assert/functions/assertThrowsInstanceOf)                       | Asserts that a function throws an instance of a constructor.                                                                                                                                         |
| [assertThrowsSame](https://evolu.dev/docs/api-reference/common/Assert/functions/assertThrowsSame)                                   | Asserts that a function throws the same value using `Object.is`.                                                                                                                                     |
| [assertTrue](https://evolu.dev/docs/api-reference/common/Assert/functions/assertTrue)                                               | Asserts that a value is exactly `true`.                                                                                                                                                              |