Bare Assert

1.2.0 · active · verified Sun Apr 19

bare-assert is a minimalist assertion library for JavaScript, designed to be lightweight and efficient. It provides fundamental assertion utilities like `ok`, `equal`, and `deepEqual`, suitable for testing or validating conditions in various JavaScript environments. The current stable version is 1.2.0, released in December 2025. This library is part of the broader Holepunchto 'bare' ecosystem, which focuses on small, modular JavaScript runtimes for desktop and mobile, suggesting an emphasis on performance and a reduced footprint. Its release cadence appears to be on an 'as-needed' basis, with updates roughly annually or semi-annually. A key differentiator is its 'bare-bones' philosophy, offering essential assertions without the extensive feature sets found in larger frameworks like Chai or Node.js's built-in `assert` module. It ships with TypeScript types, leveraging TypeScript's `asserts` keyword for enhanced type narrowing.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the default assert object and individual assertion functions, applying basic and deep equality checks, and leveraging TypeScript's `asserts` keyword for type narrowing at runtime.

import assert, { ok, equal, deepEqual } from 'bare-assert';

function processData(data: unknown): asserts data is { id: number, name: string } {
  ok(data !== null && typeof data === 'object', 'Data must be an object.');
  equal(typeof (data as any).id, 'number', 'Data must have a numeric ID.');
  equal(typeof (data as any).name, 'string', 'Data must have a string name.');
}

try {
  const validData = { id: 123, name: 'Alice' };
  processData(validData);
  console.log('Valid data processed:', validData.name); // TypeScript knows validData is now { id: number, name: string }

  ok(1 + 1 === 2, 'Arithmetic works!');
  equal('hello', 'hello', 'Strings should be equal.');
  deepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } }, 'Deep equality works for objects.');

  const invalidData = { id: 456, title: 'Invalid' };
  // This will throw an error and be caught
  // processData(invalidData); // Uncomment to see error

} catch (error: any) {
  console.error('Assertion failed:', error.message);
}

// Example of using an individual named export
function checkConfig(config: { API_KEY?: string }): asserts config is { API_KEY: string } {
  ok(config.API_KEY, 'API_KEY must be defined in config.');
}

const myConfig = { API_KEY: process.env.MY_API_KEY ?? 'default-key' };
try {
  checkConfig(myConfig);
  console.log('API Key is:', myConfig.API_KEY);
} catch (error: any) {
  console.error('Config error:', error.message);
}

view raw JSON →