{"id":"f944ce2f2e759671","repo":"vitest-dev/vitest","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/jest-asymmetric-matchers.ts","lineNumber":346,"sourceCode":"\n    return this.inverse ? !result : result\n  }\n\n  toString() {\n    return `String${this.inverse ? 'Not' : ''}Matching`\n  }\n\n  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: number) {\n    if (!isA('Number', other)) {\n      return false\n    }\n\n    let result = false\n    if (","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/expect/src/jest-asymmetric-matchers.ts#L328-L364","documentation":"Thrown by the CloseTo asymmetric matcher constructor when the sample is not a number (isA('Number') fails). closeTo asserts the received number is within a tolerance of the sample, so the expected value must be numeric.","triggerScenarios":"Constructing expect.closeTo(value) (or .not.closeTo) where value is a string, object, null, etc. The first guard in the constructor throws.","commonSituations":"Passing a string that looks numeric ('3.14'); passing undefined; a variable typed loosely that holds a non-number after a refactor.","solutions":["Pass a numeric sample: expect.closeTo(3.14159, 4).","Coerce with Number() only if you are certain it is numeric.","Use a different matcher for non-numeric closeness."],"exampleFix":"// before\nexpect(result).toEqual(expect.closeTo('3.14', 2))\n// after\nexpect(result).toEqual(expect.closeTo(3.14, 2))","handlingStrategy":"type-guard","validationCode":"function asNumber(v: unknown): number {\n  if (typeof v !== 'number' || Number.isNaN(v)) {\n    throw new TypeError(`closeTo needs a number, got ${typeof v}`)\n  }\n  return v\n}\nexpect(result).toEqual(expect.closeTo(asNumber(sample), precision))","typeGuard":"function isFiniteNumber(v: unknown): v is number {\n  return typeof v === 'number' && !Number.isNaN(v)\n}","tryCatchPattern":null,"preventionTips":["Type the sample argument of closeTo as number.","Reject stringified numbers explicitly rather than relying on coercion.","Guard NaN inputs separately since isA('Number') still returns true for NaN."],"tags":["expect","asymmetric-matcher","assertion","typescript"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}