Files
bun.sh/test/js/node/timers.promises/timers.promises.test.ts
Grigory 6bbd1e0685 fix(NodeValidator): make object check less strict (#19047)
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
2025-05-30 22:10:29 -07:00

66 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { setImmediate, setTimeout } from "node:timers/promises";
describe("setTimeout", () => {
it("abort() does not emit global error", async () => {
let unhandledRejectionCaught = false;
const catchUnhandledRejection = () => {
unhandledRejectionCaught = true;
};
process.on("unhandledRejection", catchUnhandledRejection);
const c = new AbortController();
global.setTimeout(() => c.abort());
await setTimeout(100, undefined, { signal: c.signal }).catch(() => "aborted");
// let unhandledRejection to be fired
await setTimeout(100);
process.off("unhandledRejection", catchUnhandledRejection);
expect(c.signal.aborted).toBe(true);
expect(unhandledRejectionCaught).toBe(false);
});
it("AbortController can be passed as the `options` argument", () => {
expect(async () => await setTimeout(0, undefined, new AbortController())).not.toThrow();
});
it("should reject promise when AbortController is aborted", async () => {
const abortController = new AbortController();
const promise = setTimeout(100, undefined, abortController);
abortController.abort();
await expect(promise).rejects.toThrow(expect.objectContaining({ name: "AbortError" }));
expect(abortController.signal.aborted).toBe(true);
});
});
describe("setImmediate", () => {
it("abort() does not emit global error", async () => {
let unhandledRejectionCaught = false;
const catchUnhandledRejection = () => {
unhandledRejectionCaught = true;
};
process.on("unhandledRejection", catchUnhandledRejection);
const c = new AbortController();
global.setImmediate(() => c.abort());
await setImmediate(undefined, { signal: c.signal }).catch(() => "aborted");
// let unhandledRejection to be fired
await setTimeout(100);
process.off("unhandledRejection", catchUnhandledRejection);
expect(c.signal.aborted).toBe(true);
expect(unhandledRejectionCaught).toBe(false);
});
});