deno/tests/unit_node/internal/_randomInt_test.ts
Garret Thompson 038d5a5331
Some checks are pending
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
fix(ext/node): avoid panic when crypto.randomInt has no arguments (#30314)
Closes https://github.com/denoland/deno/issues/30313
2025-08-05 17:09:39 +02:00

36 lines
1 KiB
TypeScript

// Copyright 2018-2025 the Deno authors. MIT license.
import { randomInt } from "node:crypto";
import { assert, assertThrows } from "@std/assert";
const between = (x: number, min: number, max: number) => x >= min && x < max;
Deno.test("[node/crypto.randomInt] No Params", () => {
assertThrows(() => randomInt(undefined as unknown as number));
});
Deno.test("[node/crypto.randomInt] One Param: Max", () => {
assert(between(randomInt(55), 0, 55));
});
Deno.test("[node/crypto.randomInt] Two Params: Max and Min", () => {
assert(between(randomInt(40, 120), 40, 120));
});
Deno.test("[node/crypto.randomInt] Max and Callback", () => {
let called = false;
randomInt(3, (_err, val) => {
called = true;
assert(between(val as number, 0, 3));
});
assert(called);
});
Deno.test("[node/crypto.randomInt] Min, Max and Callback", () => {
randomInt(3, 5, (_err, val) => {
assert(between(val as number, 3, 5));
});
});
Deno.test("[node/crypto.randomInt] Min is bigger than Max", () => {
assertThrows(() => randomInt(45, 34));
});