jestjs/jest · error · RangeError
${pkg}: ${name} value ${arg} is not a safe integer
Error message
${pkg}: ${name} value ${arg} is not a safe integer What it means
After confirming the value is a number, validateLength checks Number.isSafeInteger and throws a RangeError if it fails. This rejects NaN, +/-Infinity, fractional lengths, and integers outside the safe 64-bit range, because the diff algorithm indexes arrays with these values and non-integer indices would silently produce wrong output.
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 8e6d128e4a)
Solutions
- Round the value to an integer before passing: use Math.trunc/Math.floor and confirm it is finite.
- Trace the source of the NaN/Infinity (e.g. a division by zero or an uninitialised numeric field) and fix the upstream computation.
- If you must handle huge ranges, split the sequences into safe-integer-sized chunks.
Example fix
// before const half = a.length / 2; // 3.5 -> RangeError diffSequence(half, b.length, isCommon, found); // after const half = Math.floor(a.length / 2); diffSequence(half, b.length, isCommon, found);
Defensive patterns
Strategy: validation
Validate before calling
import diffSequence from '@jest/diff-sequences';
function assertSafeNonNegInt(name, v) {
if (!Number.isSafeInteger(v)) {
throw new RangeError(`${name} must be a safe integer, got ${v}`);
}
if (v < 0) throw new RangeError(`${name} must be >= 0, got ${v}`);
}
assertSafeNonNegInt('aLength', aLen);
assertSafeNonNegInt('bLength', bLen);
diffSequence(aLen, bLen, isCommon, found); Type guard
function isSafeNonNegativeInteger(v: unknown): v is number {
return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;
} Prevention
- Round lengths with Math.floor/Math.trunc before passing.
- Guard against NaN/Infinity from divisions: `Number.isFinite(x)` first.
- Compute lengths from `.length` properties directly rather than arithmetic where possible.
When it happens
Trigger: Calling diffSequence with a length that is NaN, Infinity, a float (e.g. 3.5), or a number beyond Number.MAX_SAFE_INTEGER. Common when a length comes from a division, parseFloat, or an arithmetic expression that yields a non-integer.
Common situations: Length derived from `a.length / 2` without rounding; parseFloat on user input that yields NaN; mixing BigInt and Number; very large computed sizes that overflow safe integer range.
Related errors
- ${pkg}: ${name} value ${arg} is a negative integer
- ${pkg}: ${name} typeof ${typeof arg} is not a number
- ${pkg}: ${name} typeof ${type} is not a function
- Expected is not a Number
- Precision is not a Number
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/4a45ae71c0460029.
Report an issue: GitHub.