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
- 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(...)`.
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
- 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.
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
- @jest/diff-sequences: ${name} value ${arg} is not a safe int
- @jest/diff-sequences: ${name} typeof ${typeof arg} is not a
- @jest/diff-sequences: ${name} typeof ${type} is not a functi
- any() expects to be passed a constructor function. Please pa
- Expected is not a string
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/2ad9fc480df1f9c1.json.
Report an issue: GitHub.