{"id":"df0b622b36b82cfa","repo":"jestjs/jest","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/jest-config/src/stringToBytes.ts","lineNumber":77,"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    } else {\n      input = Number.parseFloat(input);\n    }\n  }\n\n  if (typeof input === 'number') {\n    if (input === 0) {\n      return 0;\n    } else if (input <= 1 && input > 0) {\n      if (percentageReference) {\n        return Math.floor(input * percentageReference);\n      } else {\n        throw new Error(\n          'For a percentage based memory limit a percentageReference must be supplied',\n        );\n      }\n    } else if (input > 1) {\n      return Math.floor(input);\n    } else {\n      throw new Error('Unexpected numerical input');\n    }\n  }\n\n  throw new Error('Unexpected input');\n}\n\n// https://github.com/import-js/eslint-plugin-import/issues/1590\nexport default stringToBytes;\n","sourceCodeStart":59,"sourceCodeEnd":93,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-config/src/stringToBytes.ts#L59-L93","documentation":"stringToBytes treats numeric inputs between 0 and 1 (exclusive) as a percentage fraction (e.g. 0.5 = 50%) and multiplies by percentageReference. If the caller passes such a fraction without supplying percentageReference, the multiplication is undefined, so this error is thrown. Whole numbers > 1 are treated as bytes; 0 returns 0.","triggerScenarios":"Calling stringToBytes(0.5) with no second argument; passing \"50%\" which becomes 0.5 internally but the caller omitted the reference; programmatic misuse where percentageReference is undefined.","commonSituations":"Using stringToBytes for --workerIdleMemoryLimit with a percentage where the calling code path does not pass total memory; misconfiguring a custom memory-limit consumer.","solutions":["Pass the percentageReference (e.g. total system bytes) as the second argument when using a percentage or fraction < 1","Use an absolute byte value (> 1) instead of a fraction if no reference is available","Use a string like '512mb' for an absolute memory size"],"exampleFix":"// before\nstringToBytes(0.5)  // 50% but no reference\n// after\nstringToBytes(0.5, os.totalmem())  // 50% of total memory","handlingStrategy":"validation","validationCode":"import * as os from 'node:os';\nfunction safeStringToBytes(input: string | number, ref?: number) {\n  const n = typeof input === 'string' ? parseFloat(input) : input;\n  if (typeof n === 'number' && n > 0 && n <= 1 && !ref) {\n    return Math.floor(n * os.totalmem());\n  }\n  return stringToBytes(input as any, ref);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass total memory when accepting percentage limits","Prefer absolute units (mb/gb) in config to avoid ambiguity"],"tags":["memory","config","validation"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}