jestjs/jest · error · Error

For a percentage based memory limit a percentageReference…

Error message

For a percentage based memory limit a percentageReference must be supplied

What it means

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.

Solutions

  1. Pass a percentageReference: stringToBytes('50%', os.totalmem())
  2. Use an absolute unit instead of a percentage: '2gb' rather than '50%'
  3. Ensure any caller wiring percentage input also forwards the reference value

Example fix

// before
stringToBytes('50%');  // throws

// after
stringToBytes('50%', require('node:os').totalmem());
Defensive patterns

Strategy: validation

Validate before calling

import * as os from 'node:os';
function safeStringToBytes(input: string, reference?: number): number {
  if (input.endsWith('%') && reference === undefined) {
    throw new Error('Percentage input requires a reference value (e.g. os.totalmem())');
  }
  return stringToBytes(input, reference ?? os.totalmem());
}

Type guard

function isPercentageInput(input: unknown): boolean {
  return typeof input === 'string' && input.trim().endsWith('%');
}

Prevention

When it happens

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

Common situations: 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.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/df0b622b36b82cfa. Report an issue: GitHub.

Appendix: 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 8e6d128e4a)