{"id":"0d8557b012ef1dd5","repo":"jestjs/jest","slug":"expected-value-must-be-a-number-or-bigint","errorCode":null,"errorMessage":"expected value must be a number or bigint","messagePattern":"expected value must be a number or bigint","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-matcher-utils/src/index.ts","lineNumber":207,"sourceCode":"        `${RECEIVED_COLOR('received')} value must be a number or bigint`,\n        printWithType('Received', actual, printReceived),\n      ),\n    );\n  }\n};\n\n/**\n * Ensures that `expected` is of type `number | bigint`\n */\nexport const ensureExpectedIsNumber = (\n  expected: unknown,\n  matcherName: string,\n  options?: MatcherHintOptions,\n): void => {\n  if (typeof expected !== 'number' && typeof expected !== 'bigint') {\n    // Prepend maybe not only for backward compatibility.\n    const matcherString = (options ? '' : '[.not]') + matcherName;\n    throw new Error(\n      matcherErrorMessage(\n        matcherHint(matcherString, undefined, undefined, options),\n        `${EXPECTED_COLOR('expected')} value must be a number or bigint`,\n        printWithType('Expected', expected, printExpected),\n      ),\n    );\n  }\n};\n\n/**\n * Ensures that `actual` & `expected` are of type `number | bigint`\n */\nexport const ensureNumbers = (\n  actual: unknown,\n  expected: unknown,\n  matcherName: string,\n  options?: MatcherHintOptions,\n): void => {","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-matcher-utils/src/index.ts#L189-L225","documentation":"Thrown by ensureExpectedIsNumber when the EXPECTED value (second argument to the matcher) is not a 'number' or 'bigint'. It is the expected-side twin of error 140: numeric matchers validate both operands. Because the expected argument is often hand-written by the test author (a literal or a config constant), this error usually points at the test code rather than production code.","triggerScenarios":"Writing a numeric matcher with a string/undefined expected operand: expect(x).toBeGreaterThan('10'); expect(count).toBeLessThan(config.limit) where config.limit is undefined; expect(n).toBeCloseTo('0.1'); expect(bigintVal).toBeGreaterThan(5) is fine, but expect(5).toBeGreaterThan(bigint) where one side is non-numeric triggers it.","commonSituations":"Typo in a config key returning undefined (config.maxRetries vs config.maxretry); reading from a JSON file as a string and forgetting to convert; destructuring the wrong field; copy-pasting a test and forgetting to update the expected literal from a string.","solutions":["Inspect the expected operand: log the exact value/typeof you pass on the right side of the matcher and fix its source.","Coerce explicitly when the source is a string/env var: expect(count).toBeLessThan(Number(config.MAX)).","If the expected value is legitimately optional, default it to a number: expect(count).toBeLessThan(config.MAX ?? Infinity).","Confirm both sides share the same numeric domain (number vs bigint) — mixing is allowed only if both are numeric."],"exampleFix":"// before\nexpect(items.length).toBeLessThan(config.maxItems); // config.maxItems is undefined\n// after\nexpect(items.length).toBeLessThan(Number(config.maxItems));","handlingStrategy":"validation","validationCode":"const expected = config.limit;\nif (typeof expected !== 'number' && typeof expected !== 'bigint') {\n  throw new Error(`expected limit must be numeric, got ${typeof expected}`);\n}\nexpect(count).toBeLessThan(expected);","typeGuard":"const isNumeric = (v: unknown): v is number | bigint =>\n  typeof v === 'number' || typeof v === 'bigint';\n\nconst limit = isNumeric(config.limit) ? config.limit : Number(config.limit);\nexpect(count).toBeLessThan(limit);","tryCatchPattern":null,"preventionTips":["Audit config keys used as expected values; log them once to confirm they are numeric.","Default optional numeric configs: const limit = config.limit ?? Infinity.","Keep expected literals as bare numbers, never quoted strings."],"tags":["matchers","type-error","numeric","jest-matcher-utils"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}