{"id":"8ca0af8b86685d1b","repo":"jestjs/jest","slug":"received-value-must-be-a-number","errorCode":null,"errorMessage":"received value must be a number","messagePattern":"received value must be a number","errorType":"validation","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 = false;\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/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/matchers.ts#L146-L182","documentation":"Thrown by `toBeCloseTo` (matchers.ts:142) when the value passed into `expect(...)` (the `received` argument) is not a number. The matcher needs to subtract `received` from `expected` to measure the difference against `Math.pow(10, -precision) / 2`, so a non-numeric received value makes the comparison meaningless. Jest validates this after the expected-value check and throws a `TypeError` with a `printWithType` hint showing the actual type.","triggerScenarios":"Calling `expect(value).toBeCloseTo(1.5)` where `value` is the result of a function that returned a string (`'1.6'`), `undefined`, `null`, an object, or a Promise that was not awaited. Common with `expect(fetchMetric()).toBeCloseTo(0.5)` where `fetchMetric` is async and missing `await`/`.resolves`.","commonSituations":"Forgetting to await an async function before asserting on its return value; consuming a DOM input's `.value` (always a string) without coercion; a stubbed/mock return value that defaulted to `undefined`; migrating from Jasmine where a custom equality tester previously coerced types.","solutions":["If the source is async, await it first or use `expect(fetchMetric()).resolves.toBeCloseTo(0.5)`.","Coerce string inputs: `expect(Number(input.value)).toBeCloseTo(0.5)`.","Add a `typeof` guard or an `expect(typeof value).toBe('number')` precondition to localize the defect.","Check the producer of the value (the function under test) — it likely has a bug returning a non-number."],"exampleFix":"// before\nexpect(getRatio()).toBeCloseTo(0.5); // getRatio returns '0.5'\n\n// after\nexpect(Number(getRatio())).toBeCloseTo(0.5);","handlingStrategy":"type-guard","validationCode":"if (typeof received !== 'number' || !Number.isFinite(received)) {\n  throw new Error(`received must be a finite number, got ${typeof received}: ${received}`);\n}\nexpect(received).toBeCloseTo(expectedVal, precision);","typeGuard":"const isFiniteNumber = (v: unknown): v is number =>\n  typeof v === 'number' && Number.isFinite(v);","tryCatchPattern":"try {\n  expect(received).toBeCloseTo(expectedVal, precision);\n} catch (e) {\n  if (e instanceof TypeError && /received value must be a number/.test(e.message)) {\n    console.error('received was', typeof received, received);\n  }\n  throw e;\n}","preventionTips":["Await async producers before asserting, or use `.resolves.toBeCloseTo(...)`.","Coerce DOM/JSON string numbers with `Number(...)` before the assertion.","Type the function under test to return `number` so TypeScript catches misuse."],"tags":["jest","expect","type-validation","assertion","numeric","async"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}