{"record":{"id":"04cd26a2f3845ffc","repo":"jestjs/jest","slug":"expected-is-not-a-number","errorCode":null,"errorMessage":"Expected is not a Number","messagePattern":"Expected is not a Number","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/asymmetricMatchers.ts","lineNumber":351,"sourceCode":"\n    return this.inverse ? !result : result;\n  }\n\n  toString() {\n    return `String${this.inverse ? 'Not' : ''}Matching`;\n  }\n\n  override getExpectedType() {\n    return 'string';\n  }\n}\n\nclass CloseTo extends AsymmetricMatcher<number> {\n  private readonly precision: number;\n\n  constructor(sample: number, precision = 2, inverse = false) {\n    if (!isA('Number', sample)) {\n      throw new Error('Expected is not a Number');\n    }\n\n    if (!isA('Number', precision)) {\n      throw new Error('Precision is not a Number');\n    }\n\n    super(sample);\n    this.inverse = inverse;\n    this.precision = precision;\n  }\n\n  asymmetricMatch(other: unknown) {\n    if (!isA<number>('Number', other)) {\n      return false;\n    }\n    let result: boolean;\n    if (\n      other === Number.POSITIVE_INFINITY &&","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/asymmetricMatchers.ts#L333-L369","documentation":"closeTo / notCloseTo build a CloseTo matcher that compares a received number to an expected number within a precision tolerance. The constructor throws an Error if the `sample` (the expected value) is not a Number, because the floating-point comparison logic only makes sense for numbers.","triggerScenarios":"Calling `expect.closeTo(value, precision?)` where value is not a number — e.g. a string, undefined, or null. Thrown at construction time, before any assertion.","commonSituations":"Passing a numeric string ('1.5') instead of a number; passing undefined from an optional config; forgetting to parse user input; expecting closeTo on a non-numeric field.","solutions":["Pass a numeric expected value: `expect.closeTo(1.5, 2)`.","Parse/coerce the source: `Number(value)` and confirm it is finite before use.","If the value may legitimately be non-numeric, branch the assertion rather than using closeTo."],"exampleFix":"// before\nexpect(result).toEqual(expect.closeTo('1.50', 2)); // string, not Number\n\n// after\nexpect(result).toEqual(expect.closeTo(1.5, 2));","handlingStrategy":"type-guard","validationCode":"function closeToSafe(value, precision = 2) {\n  if (typeof value !== 'number' || !Number.isFinite(value)) {\n    throw new TypeError('closeTo requires a finite number');\n  }\n  return expect.closeTo(value, precision);\n}","typeGuard":"function isFiniteNumber(v: unknown): v is number {\n  return typeof v === 'number' && Number.isFinite(v);\n}\n\nconst m = isFiniteNumber(value) ? expect.closeTo(value, precision) : expect.anything();","tryCatchPattern":null,"preventionTips":["Pass numeric literals to closeTo; parse numeric strings with Number() first.","Reject NaN/Infinity upstream rather than letting them reach closeTo.","Default optional expected values to a real number, not undefined."],"tags":["expect","asymmetric-matcher","typeerror","numeric"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}