jestjs/jest · error · Error

Precision is not a Number

Error message

Precision is not a Number

What it means

CloseTo's second constructor parameter is precision (default 2); it is validated with isA('Number', ...). closeTo compares numbers to a given number of decimal places, so a non-numeric precision has no meaning and throws at construction.

Source

Thrown at packages/expect/src/asymmetricMatchers.ts:355

  toString() {
    return `String${this.inverse ? 'Not' : ''}Matching`;
  }

  override 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: unknown) {
    if (!isA<number>('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 f49721c78e)

Solutions

  1. Pass a number for precision or omit it (defaults to 2): expect.closeTo(5) or expect.closeTo(5, 3).
  2. Coerce validated config: expect.closeTo(5, Number(config.precision)).
  3. Do not pass an options object — the signature is closeTo(expected, precision?).

Example fix

// before
expect(value).toEqual(expect.closeTo(5, { precision: 2 }));

// after
expect(value).toEqual(expect.closeTo(5, 2));
Defensive patterns

Strategy: type-guard

Validate before calling

if (precision !== undefined && typeof precision !== 'number' && !(precision instanceof Number)) {
  throw new TypeError('expect.closeTo precision must be a number');
}
expect.closeTo(sample, precision);

Type guard

function isPrecision(x: unknown): x is number {
  return x === undefined || typeof x === 'number' || x instanceof Number;
}

Try / catch

// constructor throws synchronously — validate precision (or omit it) before calling

Prevention

When it happens

Trigger: Calling expect.closeTo(5, '2'), expect.closeTo(5, undefined), expect.closeTo(5, null), or expect.closeTo(5, {digits:2}). Omitting precision is fine (defaults to 2).

Common situations: Reading precision from config as a string; passing an options object instead of a number; forgetting that the 2nd arg is precision, not options.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/b8897cbd14639411.json. Report an issue: GitHub.