{"id":"e6248eef8af86515","repo":"vitest-dev/vitest","slug":"precision-is-not-a-number","errorCode":null,"errorMessage":"Precision is not a Number","messagePattern":"Precision is not a Number","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/jest-asymmetric-matchers.ts","lineNumber":350,"sourceCode":"  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 (\n      other === Number.POSITIVE_INFINITY\n      && this.sample === Number.POSITIVE_INFINITY\n    ) {\n      result = true // Infinity - Infinity is NaN","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/expect/src/jest-asymmetric-matchers.ts#L332-L368","documentation":"Thrown by the CloseTo asymmetric matcher constructor when the precision argument is not a number. Precision controls the tolerance (10 ** -precision / 2); a non-numeric precision makes the comparison undefined.","triggerScenarios":"Constructing expect.closeTo(sample, precision) where precision is a string, object, null, etc. The default is 2, but passing an explicit non-number triggers the second constructor guard.","commonSituations":"Passing precision as a string ('2'); passing an options object by mistake; refactor introducing a non-number precision variable.","solutions":["Pass a numeric precision or omit it to use the default of 2.","Ensure the precision variable is typed as number.","If you derived precision from user input, coerce and validate it first."],"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 asPrecision(v: unknown): number {\n  if (typeof v !== 'number' || !Number.isInteger(v) || v < 0) {\n    throw new TypeError(`closeTo precision must be a non-negative integer, got ${typeof v}`)\n  }\n  return v\n}\nexpect(result).toEqual(expect.closeTo(sample, asPrecision(p)))","typeGuard":"function isNonNegativeInt(v: unknown): v is number {\n  return typeof v === 'number' && Number.isInteger(v) && v >= 0\n}","tryCatchPattern":null,"preventionTips":["Pass precision as a number literal or omit it (default 2).","Type precision as number in shared assertion utilities.","Validate user-derived precision before passing to closeTo."],"tags":["expect","asymmetric-matcher","assertion","typescript"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}