jestjs/jest · error · RangeError

@jest/diff-sequences: ${name} value ${arg} is a negative int

Error message

@jest/diff-sequences: ${name} value ${arg} is a negative integer

What it means

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.

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 f49721c78e)

Solutions

  1. Pass non-negative lengths only; clamp with `Math.max(0, len)` if a computation can go negative.
  2. Double-check that you are passing lengths (counts), not indices or offsets.
  3. Add an assertion at the call site: `if (aLength < 0 || bLength < 0) throw new RangeError(...)`.

Example fix

// before
diffSequence(start - end, other.length, isCommon, foundSubsequence); // can be negative

// after
diffSequence(Math.max(0, end - start), other.length, isCommon, foundSubsequence);
Defensive patterns

Strategy: validation

Validate before calling

if (aLength < 0 || bLength < 0) {
  throw new RangeError('diffSequence lengths cannot be negative');
}

Type guard

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

Try / catch

try {
  diffSequence(aLength, bLength, isCommon, foundSubsequence);
} catch (e) {
  if (e instanceof RangeError && /negative integer/.test(e.message)) { /* clamp via Math.max(0, x) */ }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/2ad9fc480df1f9c1.json. Report an issue: GitHub.