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
| Option | Type | Default | Description |
|---|---|---|---|
checkDtypes | boolean | true | Compare dtype of both Series |
checkIndex | boolean | true | Compare row index labels |
checkNames | boolean | true | Compare Series name and index name |
checkExact | boolean | false | Exact numeric equality (no tolerance) |
rtol | number | 1e-5 | Relative tolerance |
atol | number | 1e-8 | Absolute tolerance |
objLabel | string | "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
| Option | Type | Default | Description |
|---|---|---|---|
checkDtypes | boolean | true | Compare column dtypes |
checkIndex | boolean | true | Compare row index labels |
checkNames | boolean | true | Compare index and column names |
checkLike | boolean | false | Ignore column order |
checkExact | boolean | false | Exact numeric equality |
rtol | number | 1e-5 | Relative tolerance |
atol | number | 1e-8 | Absolute tolerance |
objLabel | string | "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
| tsb | pandas |
|---|---|
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) |
AssertionError | AssertionError (Python built-in) |