{"id":"7f1e8b2f7c0e1ef8","repo":"vitest-dev/vitest","slug":"for-a-percentage-based-memory-limit-a-percentagere","errorCode":null,"errorMessage":"For a percentage based memory limit a percentageReference must be supplied","messagePattern":"For a percentage based memory limit a percentageReference must be supplied","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/utils/memory-limit.ts","lineNumber":91,"sourceCode":"          case 'gib':\n            return numericValue * 1024 * 1024 * 1024\n        }\n      }\n\n      // It ends in some kind of char so we need to do some parsing\n    }\n    else {\n      input = Number.parseFloat(input)\n    }\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":73,"sourceCodeEnd":106,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/utils/memory-limit.ts#L73-L106","documentation":"stringToBytes (memory-limit.ts:85-94) interprets a numeric input between 0 and 1 (exclusive of 0, inclusive of 1) as a fraction/percentage of a reference total. If no percentageReference is passed, the fraction cannot be converted to bytes and the function throws this error.","triggerScenarios":"Calling stringToBytes with a fractional value (e.g. 0.25) derived from a '%' string or a raw number, without supplying the second percentageReference argument. Internally, '%' inputs are divided by 100 into a fraction then routed to this branch.","commonSituations":"A vmMemoryLimit value like '50%' reaching stringToBytes without a reference (caller bug), or a custom caller of stringToBytes passing 0.5 directly. The public config path normally supplies total system memory as the reference, so hitting this usually means an internal caller forgot the argument.","solutions":["Pass a percentageReference (e.g. total bytes from os.totalmem()) whenever the input may be fractional or '%'-based.","If configuring vmMemoryLimit, use an absolute value (e.g. '1GB', 1073741824) instead of a percentage.","If you are calling stringToBytes directly, always provide the second argument for percentage inputs."],"exampleFix":"// before: fraction without reference\nconst bytes = stringToBytes('50%')\n\n// after: supply reference, or use absolute\nconst bytes = stringToBytes('50%', os.totalmem())\n// or\nconst bytes = stringToBytes('1GB')","handlingStrategy":"validation","validationCode":"function validateMemoryLimitInput(input: string | number, percentageReference?: number) {\n  const numeric = typeof input === 'string' ? Number.parseFloat(input.replace(/[^0-9.-]/g, '')) : input\n  if (typeof numeric === 'number' && numeric > 0 && numeric <= 1 && !percentageReference) {\n    throw new Error('A percentageReference (e.g. os.totalmem()) is required for fractional/percentage limits')\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass os.totalmem() as percentageReference when inputs may be '%'.","Prefer absolute vmMemoryLimit values ('1GB', bytes) to avoid ambiguity.","Validate user-supplied memory strings before they reach stringToBytes."],"tags":["memory-limit","config","percentage","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}