vitest-dev/vitest · error · Error
Unexpected numerical input for "memoryLimit"
Error message
Unexpected numerical input for "memoryLimit"
What it means
stringToBytes converts a memory limit (a string like '1GB', '50%', or a raw number) into bytes. After parsing, a numeric value must be either a fraction in (0, 1] (read as a percentage of a reference) or a whole number of bytes greater than 1. Any other numeric result has no valid meaning, so the function throws this error instead of returning a broken limit. Note the value 1 exactly is treated as 100%, not 1 byte.
Solutions
- Set the limit to a positive fraction (0,1] for a percentage, e.g. 0.5 or '50%', or a value greater than 1 for bytes, e.g. '1GB' or 1073741824.
- Omit the option entirely to fall back to the default from getWorkerMemoryLimit (1 / maxWorkers of system memory).
- If the value is computed dynamically, clamp it before assignment: Math.max(input, aSmallPositive) or treat values <= 0 as undefined.
Example fix
// before
test: { poolOptions: { threads: { memoryLimit: '0' } } }
// after
test: { poolOptions: { threads: { memoryLimit: '1GB' } } } Defensive patterns
Strategy: validation
Validate before calling
function isValidMemoryLimit(input) {
if (input == null) return true // defers to the default
const n = typeof input === 'number' ? input : Number.parseFloat(String(input))
return !Number.isNaN(n) && n > 0
}
// usage:
if (!isValidMemoryLimit(config.memoryLimit)) throw new Error('memoryLimit must be positive') Type guard
function isPositiveMemoryLimit(v: unknown): v is number | string {
if (v == null || (typeof v !== 'number' && typeof v !== 'string')) return false
const n = typeof v === 'number' ? v : Number.parseFloat(v)
return !Number.isNaN(n) && n > 0
} Prevention
- Validate any computed memory limit before assigning it to config.
- Clamp computed values with Math.max(input, smallPositive) or coerce non-positive values to undefined.
- Treat 0 as 'unset' in your own config layer rather than forwarding it to stringToBytes.
- Remember 1 means 100% in this API, not 1 byte; document that at the call site.
When it happens
Trigger: Calling stringToBytes with a value that parses to a non-positive number: 0, '0', -1, '-512mb', '-50%', or anything Number.parseFloat reduces to NaN/negative. The else branch at memory-limit.ts:99-101 is reached only when the numeric input is not > 0 (after the percentage branch (0,1] and the bytes branch >1 are excluded).
Common situations: Setting poolOptions.threads.memoryLimit or vmMemoryLimit to '0' meaning 'no limit'; a typo like '-2048mb'; env-var-driven config that resolves to an empty or negative value; arithmetic that underflows to zero such as (totalMem - totalMem); negative percentage inputs.
Related errors
- All browser instances within a project must use the same…
- `bench.compare()` expects every argument to be the return…
- `bench.compare()` requires at least 2 benchmarks, received
- `bench()` expects a benchmark function. Call `bench(name…
- `bench.from()` expects a string path or a function…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/d290d84258712e12.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/utils/memory-limit.ts:100
}
}
if (typeof input === 'number') {
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 for "memoryLimit"')
}
}
return null
}
View on GitHub (pinned to 1fa9837ec2)