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

  1. Convert with Number(value) or parseFloat(value) before passing.
  2. Use expect.toBeCloseTo(n, digits) on the value itself instead of the asymmetric matcher.
  3. Fix the data source so the value is a real number.
  4. 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

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


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)