{"id":"2ad9fc480df1f9c1","repo":"jestjs/jest","slug":"jest-diff-sequences-name-value-arg-is-a-ne","errorCode":null,"errorMessage":"@jest/diff-sequences: ${name} value ${arg} is a negative integer","messagePattern":"@jest/diff-sequences: (.+?) value (.+?) is a negative integer","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"packages/diff-sequences/src/index.ts","lineNumber":767,"sourceCode":"      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(\n  aLength: number,\n  bLength: number,\n  isCommon: IsCommon,","sourceCodeStart":749,"sourceCodeEnd":785,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/diff-sequences/src/index.ts#L749-L785","documentation":"validateLength's third guard rejects negative integers. Lengths represent counts of items in a sequence and cannot be negative; the algorithm's forward/reverse index intervals would be empty or inverted otherwise.","triggerScenarios":"Passing a computed length that went negative, e.g., `aLength - bLength` used by mistake, or a pre-decremented index, or a sentinel like -1 returned by an indexOf-style helper.","commonSituations":"Off-by-one in custom diff wrappers; passing an index/offset where a length is expected; subtracting counts and forwarding the difference as a length.","solutions":["Pass non-negative lengths only; clamp with `Math.max(0, len)` if a computation can go negative.","Double-check that you are passing lengths (counts), not indices or offsets.","Add an assertion at the call site: `if (aLength < 0 || bLength < 0) throw new RangeError(...)`."],"exampleFix":"// before\ndiffSequence(start - end, other.length, isCommon, foundSubsequence); // can be negative\n\n// after\ndiffSequence(Math.max(0, end - start), other.length, isCommon, foundSubsequence);","handlingStrategy":"validation","validationCode":"if (aLength < 0 || bLength < 0) {\n  throw new RangeError('diffSequence lengths cannot be negative');\n}","typeGuard":"function isNonNegLength(x: unknown): x is number {\n  return typeof x === 'number' && Number.isSafeInteger(x) && x >= 0;\n}","tryCatchPattern":"try {\n  diffSequence(aLength, bLength, isCommon, foundSubsequence);\n} catch (e) {\n  if (e instanceof RangeError && /negative integer/.test(e.message)) { /* clamp via Math.max(0, x) */ }\n  throw e;\n}","preventionTips":["Use Math.max(0, n) when a subtraction could go negative.","Distinguish indices (0-based) from lengths (counts) in naming and types.","Assert non-negativity before forwarding a computed length."],"tags":["diff-sequences","validation","range-error","precondition"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}