vitest-dev/vitest · error · Error
Unexpected numerical input for "memoryLimit"
Error message
Unexpected numerical input for "memoryLimit"
What it means
stringToBytes (memory-limit.ts:99-101) throws when the numeric input is not a positive value: a number <= 0, NaN, or otherwise invalid. The branch handles the leftover case after the fraction (0,1] and the absolute (>1) cases are excluded.
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 d568f8ce37)
Solutions
- Use a positive absolute value or a valid percentage string for vmMemoryLimit.
- Omit vmMemoryLimit entirely (or set to undefined) to let Vitest compute a sensible default.
- Sanitize external/env inputs before assigning them to vmMemoryLimit.
Example fix
// before: invalid value
export default defineConfig({ test: { vmMemoryLimit: 0 } })
// or env-derived
const limit = Number(process.env.LIMIT) // NaN
// after: valid or omitted
export default defineConfig({ test: { vmMemoryLimit: '1GB' } })
// or sanitize
const limit = Number(process.env.LIMIT)
export default defineConfig({ test: Number.isFinite(limit) && limit > 1 ? { vmMemoryLimit: limit } : {} }) Defensive patterns
Strategy: validation
Validate before calling
function validateMemoryLimitNumber(input: unknown) {
if (typeof input !== 'string' && typeof input !== 'number') return
const numeric = typeof input === 'string' ? Number.parseFloat(input) : input
if (!Number.isFinite(numeric) || numeric <= 0) {
throw new Error(`memoryLimit must be a positive number or a valid memory string; received '${String(input)}'`)
}
} Prevention
- Use positive absolute values or valid percentage strings for vmMemoryLimit.
- Sanitize env-derived values with Number.isFinite before assigning.
- Omit vmMemoryLimit to accept the computed default rather than setting 0.
When it happens
Trigger: Providing a vmMemoryLimit (or calling stringToBytes) with 0, a negative number, or a string that parses to NaN/0 — e.g. '0', '-512', 'abc', or an empty numeric portion.
Common situations: Typing vmMemoryLimit: 0 thinking it disables the limit; a negative value from an env-var parse; a malformed string like '--gb' whose numeric portion is empty and resolves to 0.
Related errors
- For a percentage based memory limit a percentageReference mu
- Environment "${name}" is not a valid environment. Path "${pa
- Tag "${tag.name}": priority must be a non-negative number.
- Unknown value for "test.listTags": ${listTags}
- Expected config.test.coverage.thresholds to be an object
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/d290d84258712e12.json.
Report an issue: GitHub.