{"id":"d7a5db5d390c2a4d","repo":"vitest-dev/vitest","slug":"bitlength-is-required","errorCode":null,"errorMessage":"bitLength is required","messagePattern":"bitLength is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"examples/profiling/src/prime-number.ts","lineNumber":7,"sourceCode":"/* eslint-disable unicorn/no-new-array */\n\nconst store: bigint[] = []\n\nexport default function getPrimeNumber(bitLength: number): bigint {\n  if (!bitLength) {\n    throw new Error('bitLength is required')\n  }\n\n  const number = randomBigInt(bitLength)\n\n  if (isPrimeNumber(number) && !store.includes(number)) {\n    store.push(number)\n\n    return number\n  }\n\n  return getPrimeNumber(bitLength)\n}\n\n/**\n * Generate random `BigInt` with given bit length\n * e.g. randomBigInt(8) -> 153n (1001 1001)\n */\nfunction randomBigInt(bitLength: number): bigint {","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/examples/profiling/src/prime-number.ts#L1-L25","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","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)."],"exampleFix":"// before\ngetPrimeNumbers(0)\ngetPrimeNumbers(Number(process.env.BITS)) // BITS unset -> NaN\n\n// after\ngetPrimeNumbers(256)\nconst bits = Number.parseInt(process.env.BITS ?? '256', 10)\nif (!Number.isInteger(bits) || bits <= 0) throw new Error('BITS must be a positive integer')\ngetPrimeNumbers(bits)","handlingStrategy":"validation","validationCode":"function assertBitLength(bitLength: number): void {\n  if (!Number.isInteger(bitLength) || bitLength <= 0) {\n    throw new TypeError(`bitLength must be a positive integer, got ${bitLength}`)\n  }\n}\nassertBitLength(bits)\ngetPrimeNumbers(bits)","typeGuard":"function isValidBitLength(v: unknown): v is number {\n  return typeof v === 'number' && Number.isInteger(v) && v > 0\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["bigint","validation","example-code","argument-error"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}