Files
bun.sh/test/js/node/timers/node-timers.test.ts
Jarred Sumner 3d8c10c116 Fixes #7148 (#9467)
* Fixes #7148

* Fix failing tests

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
2024-03-17 06:20:24 -07:00

55 lines
1.5 KiB
TypeScript

import { describe, test, it, expect } from "bun:test";
import { setTimeout, clearTimeout, setInterval, clearInterval, setImmediate } from "node:timers";
import { promisify } from "util";
for (const fn of [setTimeout, setInterval]) {
describe(fn.name, () => {
test("unref is possible", done => {
const timer = fn(() => {
done(new Error("should not be called"));
}, 1).unref();
const other = fn(() => {
clearInterval(other);
done();
}, 2);
if (fn === setTimeout) clearTimeout(timer);
if (fn === setInterval) clearInterval(timer);
});
});
}
it("node.js util.promisify(setTimeout) works", async () => {
const setTimeout = promisify(globalThis.setTimeout);
await setTimeout(1);
expect(async () => {
await setTimeout(1).then(a => {
throw new Error("TestPassed");
});
}).toThrow("TestPassed");
});
it("node.js util.promisify(setInterval) works", async () => {
const setInterval = promisify(globalThis.setInterval);
var runCount = 0;
const start = performance.now();
for await (const run of setInterval(1)) {
if (runCount++ === 9) break;
}
const end = performance.now();
expect(runCount).toBe(10);
expect(end - start).toBeGreaterThan(9);
});
it("node.js util.promisify(setImmediate) works", async () => {
const setImmediate = promisify(globalThis.setImmediate);
await setImmediate();
expect(async () => {
await setImmediate().then(a => {
throw new Error("TestPassed");
});
}).toThrow("TestPassed");
});