{"record":{"id":"25776f275d990fbc","repo":"jestjs/jest","slug":"received-color-received-value-must-be-a-numb","errorCode":null,"errorMessage":"${RECEIVED_COLOR('received')} 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":164,"sourceCode":"    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\n    let pass: boolean;\n    let expectedDiff = 0;\n    let receivedDiff = 0;\n\n    if (\n      received === Number.POSITIVE_INFINITY &&\n      expected === Number.POSITIVE_INFINITY\n    ) {\n      pass = true; // Infinity - Infinity is NaN\n    } else if (","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L146-L182","documentation":"Thrown by `toBeCloseTo` (matchers.ts:163) when the `received` value — what you passed to `expect(...)` — is not a number. This is a type guard on the actual/observed value, fired after the expected-value check. The matcher cannot do floating-point math on a non-number, so it aborts before producing a misleading pass/fail.","triggerScenarios":"Calling `expect(value).toBeCloseTo(0.3)` where `value` is a string, null, undefined, an object, an array, or NaN. Common when the function under test returns undefined on an error path, or returns a string like '0.30000'.","commonSituations":"Async function resolved to undefined because a branch returned early; a parser/formatter returned a string instead of a number; a destructuring bug left the variable undefined; a Number.parseInt misuse produced NaN.","solutions":["Verify the function under test actually returns a number for the given input (add a console.log or a type check before expect).","If the source genuinely returns a string, coerce with Number()/parseFloat() inside the assertion: expect(Number(result)).toBeCloseTo(...).","Handle the undefined/error path separately rather than asserting on it, e.g. assert the value is defined first.","If NaN is expected to be invalid, use toBeNaN instead of toBeCloseTo."],"exampleFix":"// before\nexpect(getPrice(item)).toBeCloseTo(9.99); // getPrice returns undefined\n\n// after\nconst price = getPrice(item);\nexpect(price).toBeDefined();\nexpect(Number(price)).toBeCloseTo(9.99);","handlingStrategy":"validation","validationCode":"const result = compute();\nassert(typeof result === 'number' && Number.isFinite(result));\nexpect(result).toBeCloseTo(0.3, 5);","typeGuard":"const isFiniteNumber = (v: unknown): v is number =>\n  typeof v === 'number' && Number.isFinite(v);","tryCatchPattern":null,"preventionTips":["Assert the source function returns a number for the test input before the matcher.","Add TypeScript return types so undefined leaks are compile errors.","Unit-test edge inputs separately so undefined returns surface early."],"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"}