tsb — testing utilities

assertSeriesEqual · assertFrameEqual · assertIndexEqual · mirrors pandas.testing

Overview

The tsb testing module provides assertion helpers for comparing tsb objects in test suites — analogous to pandas.testing.assert_series_equal, assert_frame_equal, and assert_index_equal.

When a check fails, a descriptive AssertionError is thrown with information about which element differed and at which position — making test failures easy to diagnose.

Import

import {
  assertSeriesEqual,
  assertFrameEqual,
  assertIndexEqual,
  AssertionError,
} from "tsb";

assertSeriesEqual(left, right, options?)

Assert that two Series contain identical values (with optional tolerance for floats).

Passing example

import { Series, assertSeriesEqual } from "tsb";

const a = new Series({ data: [1, 2, 3], name: "x" });
const b = new Series({ data: [1, 2, 3], name: "x" });
assertSeriesEqual(a, b);
// ✅ no exception thrown

Failing example

const c = new Series({ data: [1, 2, 99], name: "x" });
assertSeriesEqual(a, c);
// ❌ AssertionError: Series: values differ at index 2 (position 2).
//    left=3, right=99

Float tolerance

const p = new Series({ data: [1.0, 2.0] });
const q = new Series({ data: [1.0 + 1e-9, 2.0] });  // tiny rounding error
assertSeriesEqual(p, q);                              // ✅ passes (within default atol=1e-8)

assertSeriesEqual(p, q, { checkExact: true });        // ❌ exact comparison fails

Options

OptionTypeDefaultDescription
checkDtypesbooleantrueCompare dtype of both Series
checkIndexbooleantrueCompare row index labels
checkNamesbooleantrueCompare Series name and index name
checkExactbooleanfalseExact numeric equality (no tolerance)
rtolnumber1e-5Relative tolerance
atolnumber1e-8Absolute tolerance
objLabelstring"Series"Error message prefix

assertFrameEqual(left, right, options?)

Assert that two DataFrames are structurally and value-identical.

Passing example

import { DataFrame, assertFrameEqual } from "tsb";

const a = DataFrame.fromColumns({ x: [1, 2], y: [3, 4] });
const b = DataFrame.fromColumns({ x: [1, 2], y: [3, 4] });
assertFrameEqual(a, b); // ✅

Ignore column order

const c = DataFrame.fromColumns({ y: [3, 4], x: [1, 2] }); // columns reversed
assertFrameEqual(a, c, { checkLike: true }); // ✅ order ignored

Options

OptionTypeDefaultDescription
checkDtypesbooleantrueCompare column dtypes
checkIndexbooleantrueCompare row index labels
checkNamesbooleantrueCompare index and column names
checkLikebooleanfalseIgnore column order
checkExactbooleanfalseExact numeric equality
rtolnumber1e-5Relative tolerance
atolnumber1e-8Absolute tolerance
objLabelstring"DataFrame"Error message prefix

assertIndexEqual(left, right, options?)

Assert that two Index objects have identical labels.

import { Index, assertIndexEqual } from "tsb";

const a = new Index(["a", "b", "c"]);
const b = new Index(["a", "b", "c"]);
assertIndexEqual(a, b); // ✅

const c = new Index(["a", "b", "z"]);
assertIndexEqual(a, c);
// ❌ AssertionError: Index: Index values differ at position 2. left=c, right=z

AssertionError

All failed assertions throw an AssertionError instance (extends Error). It can be caught explicitly or used with expect().toThrow(AssertionError) in bun:test.

import { AssertionError, assertSeriesEqual, Series } from "tsb";

try {
  assertSeriesEqual(
    new Series({ data: [1, 2, 3] }),
    new Series({ data: [1, 2, 4] }),
  );
} catch (e) {
  if (e instanceof AssertionError) {
    console.error("Assertion failed:", e.message);
  }
}

💡 In bun:test, use expect(() => assertSeriesEqual(a, b)).toThrow(AssertionError) to write negative assertions.

pandas equivalents

tsbpandas
assertSeriesEqual(a, b)pd.testing.assert_series_equal(a, b)
assertFrameEqual(a, b)pd.testing.assert_frame_equal(a, b)
assertIndexEqual(a, b)pd.testing.assert_index_equal(a, b)
AssertionErrorAssertionError (Python built-in)