awslabs/llrt · error · Error
Expected is not a Number
Error message
Expected is not a Number
What it means
CloseTo compares numbers with a decimal precision, so both its sample and precision must be numbers. The constructor throws this plain Error when the sample is not a number, before any comparison can happen.
Solutions
- Convert with Number(value) or parseFloat(value) before passing.
- Use expect.toBeCloseTo(n, digits) on the value itself instead of the asymmetric matcher.
- Fix the data source so the value is a real number.
- Ensure argument order: expect.closeTo(sample, precision) with both numeric.
Example fix
// before
expect(val).toEqual(expect.closeTo('3.14'));
// after
expect(val).toEqual(expect.closeTo(Number('3.14'))); Defensive patterns
Strategy: validation
Validate before calling
if (typeof sample !== 'number' || Number.isNaN(sample)) throw new TypeError('closeTo sample must be a finite number'); Type guard
const isNum = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
Try / catch
try { expect(v).toEqual(expect.closeTo(n)); } catch (e) { /* n was not a number */ } Prevention
- Coerce numeric strings with Number()/parseFloat before use
- Prefer toBeCloseTo on the value directly
- Keep sample and precision argument order straight
- Type numeric test fixtures as number
When it happens
Trigger: expect.closeTo('1.5'), expect.closeTo(null), expect.closeTo(undefined), or expect.not.closeTo with a non-number sample.
Common situations: Passing numeric strings parsed from JSON/config that were never converted, or NaN-producing variables, or mixing up argument order so a precision lands in the sample slot.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Precision is not a Number
- Expected is not a string
- Expected is not a String or a RegExp
- You must provide an object to
- You must provide an array to
AI-assisted analysis of awslabs/llrt@742fc00b82 (2026-09-12).
Data as JSON: /api/errors/36268a9a4afb069d.
Report an issue: GitHub.
Appendix: source
Thrown at llrt_core/src/modules/js/@llrt/expect/jest-asymmetric-matchers.ts:301
const result = isA("String", other) && this.sample.test(other);
return this.inverse ? !result : result;
}
toString() {
return `String${this.inverse ? "Not" : ""}Matching`;
}
getExpectedType() {
return "string";
}
}
class CloseTo extends AsymmetricMatcher<number> {
private readonly precision: number;
constructor(sample: number, precision = 2, inverse = false) {
if (!isA("Number", sample)) throw new Error("Expected is not a Number");
if (!isA("Number", precision)) throw new Error("Precision is not a Number");
super(sample);
this.inverse = inverse;
this.precision = precision;
}
asymmetricMatch(other: number) {
if (!isA("Number", other)) return false;
let result = false;
if (
other === Number.POSITIVE_INFINITY &&
this.sample === Number.POSITIVE_INFINITY
) {
result = true; // Infinity - Infinity is NaN
} else if (View on GitHub (pinned to 742fc00b82)