jestjs/jest · error · Error
Unexpected input
Error message
Unexpected input
What it means
The terminal throw at the end of stringToBytes, reached when none of the preceding branches return. This happens when input is a string whose trailing characters do not match any known unit (%, kb, k, kib, mb, m, mib, gb, g, gib), or when the regex match fails to capture numericString/trailingChars for a non-numeric-ending input.
Solutions
- Use a supported unit: kb, kib, mb, mib, gb, gib, or their single-letter forms (k, m, g), or %
- Strip whitespace and commas before calling stringToBytes
- If you need a larger unit, pre-convert to gb (e.g. 2tb -> 2048gb)
Example fix
// before
stringToBytes('512tb');
// after
stringToBytes('524288gb'); Defensive patterns
Strategy: validation
Validate before calling
const SUPPORTED_UNITS = ['%', 'kb', 'kib', 'mb', 'mib', 'gb', 'gib', 'k', 'm', 'g'];
function assertSupportedUnit(input: string): void {
const match = input.match(/(.*?)([^\d.-]+)$/i);
const unit = match?.[2]?.toLowerCase();
if (unit && !SUPPORTED_UNITS.includes(unit)) {
throw new Error(`Unsupported memory unit '${unit}'. Supported: ${SUPPORTED_UNITS.join(', ')}`);
}
} Type guard
const UNIT_RE = /^(\d+(?:\.\d+)?)(kib|mib|gib|kb|mb|gb|k|m|g|%)$/i;
function isSupportedMemoryString(value: unknown): boolean {
return typeof value === 'string' && UNIT_RE.test(value.trim());
} Prevention
- Restrict the unit vocabulary at the input boundary
- Strip whitespace and commas from memory strings before parsing
- Convert unsupported units (tb, pb) to gb upstream
When it happens
Trigger: Passing an unrecognized unit like '512xb', '5tb', or '5tb'; a string like 'abc' where the last char is non-numeric but no valid unit is captured; or input of an unexpected type that bypassed all branches.
Common situations: Typos in memory units; unsupported units (terabytes, bits); copy-pasting values formatted with spaces or commas that break the regex.
Related errors
- For a percentage based memory limit a percentageReference…
- Unexpected numerical 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/a029a0876a121837.
Report an issue: GitHub.
Appendix: source
Thrown at packages/jest-config/src/stringToBytes.ts:88
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)