{"record":{"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/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-config/src/stringToBytes.ts#L59-L93","documentation":"Thrown by stringToBytes when the parsed numeric input falls in the range (0, 1] (i.e. a fractional/percentage value) but no percentageReference number was supplied as the second argument. Without a reference, the function cannot compute an absolute byte count from a fraction.","triggerScenarios":"Passing '50%' or 0.5 to stringToBytes without a second argument. The '%' branch divides by 100, producing 0.5, which enters the (0,1] branch and demands a reference. Any value 0 < n <= 1 hits this path.","commonSituations":"Custom workers calling stringToBytes for memory limits with percentage input but forgetting the reference (e.g. total system memory from os.totalmem()); misconfiguring --workerIdleMemoryLimit as a percentage in a code path that omits the reference.","solutions":["Pass a percentageReference: stringToBytes('50%', os.totalmem())","Use an absolute unit instead of a percentage: '2gb' rather than '50%'","Ensure any caller wiring percentage input also forwards the reference value"],"exampleFix":"// before\nstringToBytes('50%');  // throws\n\n// after\nstringToBytes('50%', require('node:os').totalmem());","handlingStrategy":"validation","validationCode":"import * as os from 'node:os';\nfunction safeStringToBytes(input: string, reference?: number): number {\n  if (input.endsWith('%') && reference === undefined) {\n    throw new Error('Percentage input requires a reference value (e.g. os.totalmem())');\n  }\n  return stringToBytes(input, reference ?? os.totalmem());\n}","typeGuard":"function isPercentageInput(input: unknown): boolean {\n  return typeof input === 'string' && input.trim().endsWith('%');\n}","tryCatchPattern":null,"preventionTips":["When accepting percentage input, always pass os.totalmem() as the reference","Document the percentageReference parameter at every call site","Prefer absolute units ('2gb') to avoid ambiguity"],"tags":["memory","config","argument-validation","worker"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}