jestjs/jest · error · Error
Precision is not a Number
Error message
Precision is not a Number
What it means
CloseTo's second constructor argument, precision (default 2), must be a Number; the constructor throws an Error if it is not. Precision controls the tolerance (10^-precision / 2) for the floating-point comparison, so a non-numeric precision would make the comparison meaningless.
Solutions
- Omit precision to use the default (2), or pass a numeric value: `expect.closeTo(1.0, 3)`.
- Coerce config-derived precision: `Number(precision)` and validate it is a finite integer.
- Double-check argument order: closeTo(expectedNumber, precisionNumber).
Example fix
// before expect(result).toEqual(expect.closeTo(1.0, '3')); // precision as string // after expect(result).toEqual(expect.closeTo(1.0, 3));
Defensive patterns
Strategy: type-guard
Validate before calling
function withPrecision(value, precision) {
if (precision !== undefined && (typeof precision !== 'number' || !Number.isFinite(precision))) {
throw new TypeError('precision must be a number');
}
return precision === undefined ? expect.closeTo(value) : expect.closeTo(value, precision);
} Type guard
function isOptionalFiniteNumber(v: unknown): v is number | undefined {
return v === undefined || (typeof v === 'number' && Number.isFinite(v));
} Prevention
- Omit the precision argument to use the default of 2 when unsure.
- Keep argument order straight: closeTo(expected, precision).
- Coerce config-driven precision with Number() and validate it is finite.
When it happens
Trigger: Calling `expect.closeTo(value, precision)` where precision is a non-number (e.g. a string like '3', undefined passed positionally, or an object). Thrown at construction time.
Common situations: Passing precision as a string from config; forgetting the default and passing an undefined second arg explicitly; off-by-one argument ordering where precision lands in the wrong slot.
Related errors
- Expected is not a Number
- any() expects to be passed a constructor function. Please…
- Expected is not a string
- Expected is not a String or a RegExp
- You must provide an array to
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/b8897cbd14639411.
Report an issue: GitHub.
Appendix: 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: boolean;
if (
other === Number.POSITIVE_INFINITY &&
this.sample === Number.POSITIVE_INFINITY
) {
result = true; // Infinity - Infinity is NaN
} else if (View on GitHub (pinned to 8e6d128e4a)