jestjs/jest · error · Error

Unexpected numerical input

Error message

Unexpected numerical input

What it means

The numeric branch in stringToBytes handles 0 (returns 0), fractions in (0,1] (percentage), and values > 1 (bytes). Any other number - negatives and NaN - falls to the else and throws 'Unexpected numerical input'. This guards against nonsensical memory limits like -1.

Source

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

      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. Provide a positive number of bytes (> 1) or a valid fraction in (0,1]
  2. Sanitize inputs to ensure they are finite and >= 0 before calling
  3. Use a descriptive string like '1gb' instead of raw numbers

Example fix

// before
stringToBytes(-1)
// after
stringToBytes('1gb')
Defensive patterns

Strategy: validation

Validate before calling

function isFiniteNonNegative(n: number): boolean {
  return Number.isFinite(n) && n >= 0;
}
if (!isFiniteNonNegative(value)) throw new Error('memory limit must be a finite non-negative number');

Prevention

When it happens

Trigger: Calling stringToBytes(-1), stringToBytes(NaN), or a parsed string that yields a negative number.

Common situations: Arithmetic that underflows to negative; parseFloat on garbage yielding NaN that flows in; config typos producing negative memory values.

Related errors


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