{"record":{"id":"4dc6fc9984782018","repo":"jestjs/jest","slug":"expected-color-expected-value-must-be-a-numb","errorCode":null,"errorMessage":"${EXPECTED_COLOR('expected')} value must be a number","messagePattern":"(.+?) value must be a number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":154,"sourceCode":"    // Passing the actual and expected objects so that a custom reporter\n    // could access them, for example in order to display a custom visual diff,\n    // or create a different error message\n    return {actual: received, expected, message, name: matcherName, pass};\n  },\n\n  toBeCloseTo(received: number, expected: number, precision = 2) {\n    const matcherName = 'toBeCloseTo';\n    const secondArgument = arguments.length === 3 ? 'precision' : undefined;\n    const isNot = this.isNot;\n    const options: MatcherHintOptions = {\n      isNot,\n      promise: this.promise,\n      secondArgument,\n      secondArgumentColor: (arg: string) => arg,\n    };\n\n    if (typeof expected !== 'number') {\n      throw new TypeError(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${EXPECTED_COLOR('expected')} value must be a number`,\n          printWithType('Expected', expected, printExpected),\n        ),\n      );\n    }\n\n    if (typeof received !== 'number') {\n      throw new TypeError(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${RECEIVED_COLOR('received')} value must be a number`,\n          printWithType('Received', received, printReceived),\n        ),\n      );\n    }\n","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L136-L172","documentation":"Thrown by the `toBeCloseTo` matcher (matchers.ts:153) when the `expected` argument (the 2nd parameter, the value to compare against) is not a number. `toBeCloseTo` performs floating-point comparison within a configurable precision, so it must compute `Math.abs(expected - received)`, which requires both operands to be numeric. This guard fires before the received-value check at line 163.","triggerScenarios":"Calling `expect(x).toBeCloseTo(expected, precision)` where `expected` is a string (e.g. '0.3'), null, undefined, an object, or NaN. Also triggered by swapping arguments so a precision number lands in the expected slot, or when expected is sourced from JSON/config as a string and never coerced.","commonSituations":"Expected value loaded from a JSON fixture or env var (always a string); expected computed by a function that returns undefined on an edge case; migrating from a different assertion library where the argument order for approximate comparison differs.","solutions":["Ensure the second argument to toBeCloseTo is a JS number, not a string: pass 0.3 not '0.3'.","If the value comes from a string source, coerce it before the assertion: Number(value) or parseFloat(value).","Check that the variable holding `expected` is not undefined due to a missing return or wrong property access.","Confirm you did not swap arguments: signature is toBeCloseTo(expected, precision), not toBeCloseTo(precision, expected)."],"exampleFix":"// before\nexpect(0.1 + 0.2).toBeCloseTo('0.3');\n\n// after\nexpect(0.1 + 0.2).toBeCloseTo(0.3);\n// or coerce a string source\nexpect(0.1 + 0.2).toBeCloseTo(Number(configValue));","handlingStrategy":"type-guard","validationCode":"const expected = config.value;\nif (typeof expected !== 'number' || !Number.isFinite(expected)) {\n  throw new Error(`toBeCloseTo expected must be a finite number, got ${expected}`);\n}\nexpect(received).toBeCloseTo(expected, 2);","typeGuard":"const isFiniteNumber = (v: unknown): v is number =>\n  typeof v === 'number' && Number.isFinite(v);","tryCatchPattern":null,"preventionTips":["Type received/expected as number in your test helpers and let TS catch mismatches at compile time.","Never source numeric expected values directly from JSON strings without Number() coercion.","Remember the argument order: toBeCloseTo(expected, precision)."],"tags":["jest","expect","matcher","type-error","numeric","tobecloseto"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}