{"record":{"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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/utils/memory-limit.ts#L73-L106","documentation":"stringToBytes converts memory limits. A numeric input in (0, 1] is interpreted as a fraction (e.g. 0.5 = 50%) and requires a percentageReference (the total to take the fraction of). If none is provided it throws. An input > 1 is treated as absolute bytes; a non-positive number throws 'Unexpected numerical input'. This is used to parse vmMemoryLimit.","triggerScenarios":"Calling stringToBytes with a fractional number like 0.5 without passing percentageReference. In Vitest this path is reached internally when computing worker memory limits from config.vmMemoryLimit; the reference (total system memory) is normally supplied, so seeing this error means an internal caller omitted it or a custom value slipped through as a raw fraction.","commonSituations":"Setting test.vmMemoryLimit to a bare decimal (e.g. 0.5) in a context where Vitest cannot supply a percentage reference, or a custom integration calling stringToBytes directly without a reference. Most users express limits as percentages via the '%' suffix or as absolute sizes (e.g. '512MB'), which do not hit this branch.","solutions":["Express the limit with a unit suffix: '50%' or '512MB' instead of a bare 0.5.","If calling stringToBytes directly, always pass percentageReference (e.g. os.totalmem()).","Use an absolute byte value greater than 1 to bypass the percentage branch entirely.","Confirm test.vmMemoryLimit is a string ('50%', '1g') rather than a raw fraction."],"exampleFix":"// before\ntest: { vmMemoryLimit: 0.5 } // bare fraction, no reference\n\n// after\ntest: { vmMemoryLimit: '50%' } // or '2g'","handlingStrategy":"validation","validationCode":"import * as os from 'node:os'\nfunction resolveLimit(input) {\n  if (typeof input === 'number' && input > 0 && input <= 1) {\n    return stringToBytes(input, os.totalmem()) // always pass a reference\n  }\n  return stringToBytes(input)\n}","typeGuard":"function isPercentageValue(v) {\n  return typeof v === 'number' && v > 0 && v <= 1\n}","tryCatchPattern":null,"preventionTips":["Express vmMemoryLimit with a unit: '50%', '2g', '512MB'.","When calling stringToBytes with a fraction, always pass percentageReference.","Prefer absolute sizes to avoid the percentage branch."],"tags":["memory","config","percentage","vmmemorylimit"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}