jestjs/jest · error · Error

For a percentage based memory limit a percentageReference mu

Error message

For a percentage based memory limit a percentageReference must be supplied

What it means

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.

Source

Thrown at packages/jest-config/src/stringToBytes.ts:77

          case 'gib':
            return numericValue * 1024 * 1024 * 1024;
        }
      }

      // It ends in some kind of char so we need to do some parsing
    } else {
      input = Number.parseFloat(input);
    }
  }

  if (typeof input === 'number') {
    if (input === 0) {
      return 0;
    } else if (input <= 1 && input > 0) {
      if (percentageReference) {
        return Math.floor(input * percentageReference);
      } else {
        throw new Error(
          'For a percentage based memory limit a percentageReference must be supplied',
        );
      }
    } else if (input > 1) {
      return Math.floor(input);
    } else {
      throw new Error('Unexpected numerical input');
    }
  }

  throw new Error('Unexpected input');
}

// https://github.com/import-js/eslint-plugin-import/issues/1590
export default stringToBytes;

View on GitHub (pinned to f49721c78e)

Solutions

  1. Pass the percentageReference (e.g. total system bytes) as the second argument when using a percentage or fraction < 1
  2. Use an absolute byte value (> 1) instead of a fraction if no reference is available
  3. Use a string like '512mb' for an absolute memory size

Example fix

// before
stringToBytes(0.5)  // 50% but no reference
// after
stringToBytes(0.5, os.totalmem())  // 50% of total memory
Defensive patterns

Strategy: validation

Validate before calling

import * as os from 'node:os';
function safeStringToBytes(input: string | number, ref?: number) {
  const n = typeof input === 'string' ? parseFloat(input) : input;
  if (typeof n === 'number' && n > 0 && n <= 1 && !ref) {
    return Math.floor(n * os.totalmem());
  }
  return stringToBytes(input as any, ref);
}

Prevention

When it happens

Trigger: 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.

Common situations: Using stringToBytes for --workerIdleMemoryLimit with a percentage where the calling code path does not pass total memory; misconfiguring a custom memory-limit consumer.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/df0b622b36b82cfa.json. Report an issue: GitHub.