{"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 = false;\n    if (\n      other === Number.POSITIVE_INFINITY &&","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/asymmetricMatchers.ts#L333-L369","documentation":"CloseTo (expect.closeTo) asserts a received number is within precision decimal places of the expected sample; the constructor validates the sample is a Number via isA('Number', ...) (covers primitive and boxed Number). Non-numbers throw immediately because the floating-point comparison is meaningless otherwise.","triggerScenarios":"Calling expect.closeTo('5', 2), expect.closeTo(undefined, 2), expect.closeTo(null, 2), or expect.closeTo({value:5}, 2).","commonSituations":"Passing a string from user input without coercing; passing undefined because the expected value came from a missing object property; mixing up argument order.","solutions":["Pass a number: expect.closeTo(5, 2).","Coerce validated input: expect.closeTo(Number(value), 2) after checking Number.isFinite.","Confirm the property you read actually exists (avoid undefined)."],"exampleFix":"// before\nexpect(result).toEqual(expect.closeTo(config.threshold, 2)); // threshold is '0.5'\n\n// after\nexpect(result).toEqual(expect.closeTo(Number(config.threshold), 2));","handlingStrategy":"type-guard","validationCode":"if (typeof sample !== 'number' && !(sample instanceof Number)) {\n  throw new TypeError('expect.closeTo needs a number for expected');\n}\nexpect.closeTo(sample, precision);","typeGuard":"function isNumberLike(x: unknown): x is number | Number {\n  return typeof x === 'number' || x instanceof Number;\n}","tryCatchPattern":"// constructor throws synchronously — validate expected before calling","preventionTips":["Pass numeric literals or Number-typed variables for expected.","Coerce after validating: Number(value) only when Number.isFinite.","Watch for undefined coming from missing object properties."],"tags":["expect","asymmetric-matcher","number","validation"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}