vitest-dev/vitest · error · Error
bitLength is required
Error message
bitLength is required
What it means
Thrown by the example profiling helper getPrimeNumbers() in examples/profiling when its bitLength argument is falsy. This is demo/benchmark code (not shipped Vitest API) that generates a random prime BigInt of a given bit length. The guard rejects 0, NaN, undefined, null, and '' because the algorithm needs a concrete positive bit length to build the binary string.
Source
Thrown at examples/profiling/src/prime-number.ts:7
/* eslint-disable unicorn/no-new-array */
const store: bigint[] = []
export default function getPrimeNumber(bitLength: number): bigint {
if (!bitLength) {
throw new Error('bitLength is required')
}
const number = randomBigInt(bitLength)
if (isPrimeNumber(number) && !store.includes(number)) {
store.push(number)
return number
}
return getPrimeNumber(bitLength)
}
/**
* Generate random `BigInt` with given bit length
* e.g. randomBigInt(8) -> 153n (1001 1001)
*/
function randomBigInt(bitLength: number): bigint {View on GitHub (pinned to d568f8ce37)
Solutions
- Pass a positive integer bit length, e.g. getPrimeNumbers(8) or getPrimeNumbers(256).
- If bitLength comes from config, validate/coerce it before calling: const bits = Number(process.env.BITS) || 256.
- Replace the truthy guard with an explicit range check if you control the file: if (!Number.isInteger(bitLength) || bitLength <= 0).
Example fix
// before
getPrimeNumbers(0)
getPrimeNumbers(Number(process.env.BITS)) // BITS unset -> NaN
// after
getPrimeNumbers(256)
const bits = Number.parseInt(process.env.BITS ?? '256', 10)
if (!Number.isInteger(bits) || bits <= 0) throw new Error('BITS must be a positive integer')
getPrimeNumbers(bits) Defensive patterns
Strategy: validation
Validate before calling
function assertBitLength(bitLength: number): void {
if (!Number.isInteger(bitLength) || bitLength <= 0) {
throw new TypeError(`bitLength must be a positive integer, got ${bitLength}`)
}
}
assertBitLength(bits)
getPrimeNumbers(bits) Type guard
function isValidBitLength(v: unknown): v is number {
return typeof v === 'number' && Number.isInteger(v) && v > 0
} Prevention
- Coerce and validate numeric inputs from env/config before passing them in.
- Treat 0 as invalid for bit-length style APIs; use an explicit positive-integer check.
- Default to a sane non-zero value (e.g. 256) when a config value is missing.
When it happens
Trigger: Calling getPrimeNumbers(0), getPrimeNumbers(undefined), getPrimeNumbers(NaN), or calling it with a value read from an unset config/env variable that defaulted to 0. The truthy check `if (!bitLength)` treats 0 as invalid even though 0 is a valid number type.
Common situations: Running the profiling example with a CLI flag that was not parsed, a default config value of 0, or a destructured property that was undefined. Also hit when a caller assumes 0 means 'pick a default'.
Related errors
- Negative numbers are not supported
- Cannot use two functions as arguments. Please use the second
- Cannot compare screenshots without a test path
- `resolveOptions` has to be used in a test file
- Invalid command name "${name}". Only alphanumeric characters
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/d7a5db5d390c2a4d.json.
Report an issue: GitHub.