{"record":{"id":"4a45ae71c0460029","repo":"jestjs/jest","slug":"pkg-name-value-arg-is-not-a-safe-integer","errorCode":null,"errorMessage":"${pkg}: ${name} value ${arg} is not a safe integer","messagePattern":"(.+?): (.+?) value (.+?) is not a safe integer","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"packages/diff-sequences/src/index.ts","lineNumber":764,"sourceCode":"      aStartFollowing,\n      aEnd,\n      bStartFollowing,\n      bEnd,\n      transposed,\n      callbacks,\n      aIndexesF,\n      aIndexesR,\n      division,\n    );\n  }\n};\n\nconst validateLength = (name: string, arg: unknown) => {\n  if (typeof arg !== 'number') {\n    throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);\n  }\n  if (!Number.isSafeInteger(arg)) {\n    throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);\n  }\n  if (arg < 0) {\n    throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);\n  }\n};\n\nconst validateCallback = (name: string, arg: unknown) => {\n  const type = typeof arg;\n  if (type !== 'function') {\n    throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);\n  }\n};\n\n// Compare items in two sequences to find a longest common subsequence.\n// Given lengths of sequences and input function to compare items at indexes,\n// return by output function the number of adjacent items and starting indexes\n// of each common subsequence.\nexport default function diffSequence(","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/diff-sequences/src/index.ts#L746-L782","documentation":"After confirming the value is a number, validateLength checks Number.isSafeInteger and throws a RangeError if it fails. This rejects NaN, +/-Infinity, fractional lengths, and integers outside the safe 64-bit range, because the diff algorithm indexes arrays with these values and non-integer indices would silently produce wrong output.","triggerScenarios":"Calling diffSequence with a length that is NaN, Infinity, a float (e.g. 3.5), or a number beyond Number.MAX_SAFE_INTEGER. Common when a length comes from a division, parseFloat, or an arithmetic expression that yields a non-integer.","commonSituations":"Length derived from `a.length / 2` without rounding; parseFloat on user input that yields NaN; mixing BigInt and Number; very large computed sizes that overflow safe integer range.","solutions":["Round the value to an integer before passing: use Math.trunc/Math.floor and confirm it is finite.","Trace the source of the NaN/Infinity (e.g. a division by zero or an uninitialised numeric field) and fix the upstream computation.","If you must handle huge ranges, split the sequences into safe-integer-sized chunks."],"exampleFix":"// before\nconst half = a.length / 2; // 3.5 -> RangeError\ndiffSequence(half, b.length, isCommon, found);\n\n// after\nconst half = Math.floor(a.length / 2);\ndiffSequence(half, b.length, isCommon, found);","handlingStrategy":"validation","validationCode":"import diffSequence from '@jest/diff-sequences';\n\nfunction assertSafeNonNegInt(name, v) {\n  if (!Number.isSafeInteger(v)) {\n    throw new RangeError(`${name} must be a safe integer, got ${v}`);\n  }\n  if (v < 0) throw new RangeError(`${name} must be >= 0, got ${v}`);\n}\n\nassertSafeNonNegInt('aLength', aLen);\nassertSafeNonNegInt('bLength', bLen);\ndiffSequence(aLen, bLen, isCommon, found);","typeGuard":"function isSafeNonNegativeInteger(v: unknown): v is number {\n  return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;\n}","tryCatchPattern":null,"preventionTips":["Round lengths with Math.floor/Math.trunc before passing.","Guard against NaN/Infinity from divisions: `Number.isFinite(x)` first.","Compute lengths from `.length` properties directly rather than arithmetic where possible."],"tags":["diff-sequences","input-validation","rangeerror","numeric"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}