awslabs/llrt · error · Error

Precision is not a Number

Error message

Precision is not a Number

What it means

CloseTo's second argument (precision, default 2) must be a number; the constructor throws this plain Error when it is not. This catches typos like passing a string digit or an options object in the precision slot.

Solutions

  1. Pass a number: expect.closeTo(3.14, 2).
  2. Coerce with Number(precision) or parseInt/parseFloat if it comes from config.
  3. Use the default precision by omitting the argument.
  4. Fix argument order so an options object isn't passed as precision.

Example fix

// before
expect(val).toEqual(expect.closeTo(3.14, '2'));
// after
expect(val).toEqual(expect.closeTo(3.14, Number('2')));
Defensive patterns

Strategy: validation

Validate before calling

if (precision !== undefined && typeof precision !== 'number') throw new TypeError('closeTo precision must be a number');

Type guard

const isPrecision = (p: unknown): p is number => typeof p === 'number' && Number.isInteger(p) && p >= 0;

Try / catch

try { expect(v).toEqual(expect.closeTo(n, digits)); } catch (e) { /* digits was not a number */ }

Prevention

When it happens

Trigger: expect.closeTo(3.14, '2') or expect.closeTo(3.14, someNonNumber) — thrown at matcher construction.

Common situations: Reading precision from config/CLI where it arrives as a string, passing an options object as the second arg, or accidental extra arguments.

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/efbf27218c5d19a9. Report an issue: GitHub.

Appendix: source

Thrown at llrt_core/src/modules/js/@llrt/expect/jest-asymmetric-matchers.ts:303

    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 (
      other === Number.NEGATIVE_INFINITY &&
      this.sample === Number.NEGATIVE_INFINITY

View on GitHub (pinned to 742fc00b82)