jestjs/jest · error · RangeError

${pkg}: ${name} value ${arg} is a negative integer

Error message

${pkg}: ${name} value ${arg} is a negative integer

What it means

validateLength's final check rejects negative safe integers with a RangeError, because negative lengths have no meaning for a sequence index range. The algorithm iterates from 0 up to the length, so a negative bound would skip all items and indicate a caller bug.

Source

Thrown at packages/diff-sequences/src/index.ts:767

      bEnd,
      transposed,
      callbacks,
      aIndexesF,
      aIndexesR,
      division,
    );
  }
};

const validateLength = (name: string, arg: unknown) => {
  if (typeof arg !== 'number') {
    throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
  }
  if (!Number.isSafeInteger(arg)) {
    throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
  }
  if (arg < 0) {
    throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
  }
};

const validateCallback = (name: string, arg: unknown) => {
  const type = typeof arg;
  if (type !== 'function') {
    throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
  }
};

// Compare items in two sequences to find a longest common subsequence.
// Given lengths of sequences and input function to compare items at indexes,
// return by output function the number of adjacent items and starting indexes
// of each common subsequence.
export default function diffSequence(
  aLength: number,
  bLength: number,
  isCommon: IsCommon,

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Clamp the length to a minimum of 0 with `Math.max(0, value)`.
  2. Fix the upstream arithmetic so it cannot underflow (re-check offsets/slices).
  3. If -1 is used as a sentinel for 'no sequence', convert it to 0 before calling diffSequence.

Example fix

// before
const len = a.length - removed; // can be -3
diffSequence(len, b.length, isCommon, found);

// after
const len = Math.max(0, a.length - removed);
diffSequence(len, b.length, isCommon, found);
Defensive patterns

Strategy: validation

Validate before calling

const safeLen = (v) => Math.max(0, Math.floor(v));
// ensure non-negative before calling diffSequence
diffSequence(safeLen(aLen), safeLen(bLen), isCommon, found);

Type guard

function isNonNegativeInteger(v: unknown): v is number {
  return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;
}

Prevention

When it happens

Trigger: Calling diffSequence with a length argument that is a negative integer (e.g. -1), typically from an arithmetic mistake such as `a.length - b.length` when b is longer, or from `Math.max`/`Math.min` applied in the wrong order.

Common situations: Subtraction that can go negative (e.g. `a.length - offset` with offset > length); slicing bounds computed incorrectly; defaulting a missing value to -1 as a sentinel.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/59b70edfc1acd10b. Report an issue: GitHub.