mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
* Fixes #7148 * Fix failing tests --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
55 lines
1.5 KiB
TypeScript
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");
|
|
});
|