jestjs/jest · error · Error
Unexpected numerical input
Error message
Unexpected numerical input
What it means
Thrown by stringToBytes when the numeric input is a number less than or equal to 0 and not exactly 0. The accepted branches are: 0 (returns 0), (0,1] (percentage), and >1 (absolute bytes). Any negative number, or values like NaN after parsing that slip past the typeof check, hit the final else.
Solutions
- Use a non-negative value for the memory limit
- Validate input >= 0 before calling stringToBytes
- If computing dynamically, clamp with Math.max(0, value)
Example fix
// before
stringToBytes('-512mb');
// after
stringToBytes('512mb'); Defensive patterns
Strategy: validation
Validate before calling
function assertNonNegativeBytes(input: string | number): void {
const num = typeof input === 'number' ? input : parseFloat(input);
if (num < 0) {
throw new Error('Memory limit must be non-negative');
}
} Type guard
function isNonNegativeMemoryValue(value: string | number): boolean {
const num = typeof value === 'number' ? value : parseFloat(value);
return !Number.isNaN(num) && num >= 0;
} Prevention
- Clamp computed memory values with Math.max(0, value)
- Validate user-supplied memory strings before passing to stringToBytes
- Reject negative values at the config/UI layer
When it happens
Trigger: Passing a negative memory value such as '-512mb' or -100; a percentage like '-10%'; or a numeric input that resolves to a negative number after unit parsing.
Common situations: Sign errors in computed memory limits; user typos adding a stray '-' before a unit; off-by-one arithmetic that produces a negative cap.
Related errors
- For a percentage based memory limit a percentageReference…
- Unexpected input
- Can't find a root directory while resolving a config file…
- "cwd" must be an absolute path. cwd
- Jest: ' ' is not a valid TypeScript configuration loader.
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/992251e883c116e3.
Report an issue: GitHub.
Appendix: source
Thrown at packages/jest-config/src/stringToBytes.ts:84
input = Number.parseFloat(input);
}
}
if (typeof input === 'number') {
if (input === 0) {
return 0;
} else if (input <= 1 && input > 0) {
if (percentageReference) {
return Math.floor(input * percentageReference);
} else {
throw new Error(
'For a percentage based memory limit a percentageReference must be supplied',
);
}
} else if (input > 1) {
return Math.floor(input);
} else {
throw new Error('Unexpected numerical input');
}
}
throw new Error('Unexpected input');
}
// https://github.com/import-js/eslint-plugin-import/issues/1590
export default stringToBytes;
View on GitHub (pinned to 8e6d128e4a)