jestjs/jest · error · RangeError
@jest/diff-sequences: ${name} value ${arg} is not a safe int
Error message
@jest/diff-sequences: ${name} value ${arg} is not a safe integer What it means
validateLength's second guard requires Number.isSafeInteger, rejecting NaN, +/-Infinity, and non-integer numbers, plus integers beyond 2^53-1. The LCS algorithm allocates index arrays and does integer arithmetic, so unsafe/large or non-finite values would silently corrupt results or overflow.
Source
Thrown at packages/diff-sequences/src/index.ts:764
aStartFollowing,
aEnd,
bStartFollowing,
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(View on GitHub (pinned to f49721c78e)
Solutions
- Ensure the length is a finite integer computed from a real collection (Array.length, String.length).
- Sanitize upstream: `if (!Number.isSafeInteger(len)) throw new RangeError('bad length')`.
- For huge inputs, chunk the sequences so each diff call stays within safe-integer range.
Example fix
// before diffSequence(Number.parseFloat(input), other.length, isCommon, foundSubsequence); // NaN or 5.5 // after const len = Number.isFinite(input) ? Math.trunc(input) : 0; diffSequence(len, other.length, isCommon, foundSubsequence);
Defensive patterns
Strategy: validation
Validate before calling
if (!Number.isSafeInteger(aLength) || !Number.isSafeInteger(bLength)) {
throw new RangeError('diffSequence lengths must be safe integers');
} Type guard
function isSafeLength(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 && /not a safe integer/.test(e.message)) { /* split input or reject */ }
throw e;
} Prevention
- Reject NaN/Infinity/fractional lengths at the boundary of your wrapper.
- For very large inputs, chunk sequences so each call stays within safe-integer range.
- Assert Number.isSafeInteger on any externally sourced length.
When it happens
Trigger: Passing a length that is NaN (e.g., reading .length off something without one), Infinity, a fractional number like 5.5, or a value > Number.MAX_SAFE_INTEGER as aLength/bLength.
Common situations: Passing the result of a broken length computation; confusing a count with a float; streaming/very-large inputs where length exceeds the safe-integer range.
Related errors
- @jest/diff-sequences: ${name} value ${arg} is a negative 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/e718e328323a8fac.json.
Report an issue: GitHub.