{"record":{"id":"d290d84258712e12","repo":"vitest-dev/vitest","slug":"unexpected-numerical-input-for-memorylimit","errorCode":null,"errorMessage":"Unexpected numerical input for \"memoryLimit\"","messagePattern":"Unexpected numerical input for \"memoryLimit\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/utils/memory-limit.ts","lineNumber":100,"sourceCode":"    }\n  }\n\n  if (typeof input === 'number') {\n    if (input <= 1 && input > 0) {\n      if (percentageReference) {\n        return Math.floor(input * percentageReference)\n      }\n      else {\n        throw new Error(\n          'For a percentage based memory limit a percentageReference must be supplied',\n        )\n      }\n    }\n    else if (input > 1) {\n      return Math.floor(input)\n    }\n    else {\n      throw new Error('Unexpected numerical input for \"memoryLimit\"')\n    }\n  }\n\n  return null\n}\n","sourceCodeStart":82,"sourceCodeEnd":106,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/utils/memory-limit.ts#L82-L106","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\ntest: { poolOptions: { threads: { memoryLimit: '0' } } }\n\n// after\ntest: { poolOptions: { threads: { memoryLimit: '1GB' } } }","handlingStrategy":"validation","validationCode":"function isValidMemoryLimit(input) {\n  if (input == null) return true // defers to the default\n  const n = typeof input === 'number' ? input : Number.parseFloat(String(input))\n  return !Number.isNaN(n) && n > 0\n}\n// usage:\nif (!isValidMemoryLimit(config.memoryLimit)) throw new Error('memoryLimit must be positive')","typeGuard":"function isPositiveMemoryLimit(v: unknown): v is number | string {\n  if (v == null || (typeof v !== 'number' && typeof v !== 'string')) return false\n  const n = typeof v === 'number' ? v : Number.parseFloat(v)\n  return !Number.isNaN(n) && n > 0\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["configuration","validation","memory","worker-threads"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}