diff options
author | 2023-02-05 16:10:31 -0800 | |
---|---|---|
committer | 2023-02-05 16:10:31 -0800 | |
commit | 5a96af6bd283d258b29d9e432e939a1969ead119 (patch) | |
tree | 29f09bf0e154a74e2de65da862715d3397dddfac /test/bun.js/setTimeout.test.js | |
parent | f913a468c2d9d57e6e21ed9f5b07d6cce12cc09b (diff) | |
parent | ca2e708be11e9481938965ee92acc38c01a7726a (diff) | |
download | bun-5a96af6bd283d258b29d9e432e939a1969ead119.tar.gz bun-5a96af6bd283d258b29d9e432e939a1969ead119.tar.zst bun-5a96af6bd283d258b29d9e432e939a1969ead119.zip |
Merge branch 'main' into jarred/new-bund-ressurected-branch
Diffstat (limited to 'test/bun.js/setTimeout.test.js')
-rw-r--r-- | test/bun.js/setTimeout.test.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/test/bun.js/setTimeout.test.js b/test/bun.js/setTimeout.test.js index 9cd16ece2..393a32bbe 100644 --- a/test/bun.js/setTimeout.test.js +++ b/test/bun.js/setTimeout.test.js @@ -89,3 +89,49 @@ it("setTimeout(() => {}, 0)", async () => { }); expect(ranFirst).toBe(-1); }); + +it("Bun.sleep", async () => { + var sleeps = 0; + await Bun.sleep(0); + const start = performance.now(); + sleeps++; + await Bun.sleep(1); + sleeps++; + await Bun.sleep(2); + sleeps++; + const end = performance.now(); + expect((end - start) * 1000).toBeGreaterThanOrEqual(3); + + expect(sleeps).toBe(3); +}); + +it("Bun.sleep propagates exceptions", async () => { + try { + await Bun.sleep(1).then(a => { + throw new Error("TestPassed"); + }); + throw "Should not reach here"; + } catch (err) { + expect(err.message).toBe("TestPassed"); + } +}); + +it("Bun.sleep works with a Date object", async () => { + var ten_ms = new Date(); + ten_ms.setMilliseconds(ten_ms.getMilliseconds() + 10); + const now = performance.now(); + await Bun.sleep(ten_ms); + expect(performance.now() - now).toBeGreaterThanOrEqual(10); +}); + +it("node.js timers/promises setTimeout propagates exceptions", async () => { + const { setTimeout } = require("timers/promises"); + try { + await setTimeout(1).then(a => { + throw new Error("TestPassed"); + }); + throw "Should not reach here"; + } catch (err) { + expect(err.message).toBe("TestPassed"); + } +}); |