diff options
author | 2023-03-02 09:00:20 -0800 | |
---|---|---|
committer | 2023-03-02 09:00:20 -0800 | |
commit | 20930849ce0bcfe685e8042a4a53d9c9dd67343b (patch) | |
tree | 2f7eaa99dd6e273bcd0e26c2adc772f2aefc7bf1 /test/bun.js/sleepSync.test.ts | |
parent | efdf64746076c4ed09da87e41d7de81c2b659dab (diff) | |
download | bun-20930849ce0bcfe685e8042a4a53d9c9dd67343b.tar.gz bun-20930849ce0bcfe685e8042a4a53d9c9dd67343b.tar.zst bun-20930849ce0bcfe685e8042a4a53d9c9dd67343b.zip |
Fix `Bun.sleepSync` to actually use milliseconds (#2242)
* Fix Bun.sleep/sleepSync to actually use milliseconds
`Bun.sleepSync` was accidentally treating its argument as seconds rather than milliseconds as the docs stated. This is a breaking change in that the function now behaves as documented. Fixed relevant tests.
* sleepSync: add more argument checking, tests
Diffstat (limited to '')
-rw-r--r-- | test/bun.js/sleepSync.test.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/test/bun.js/sleepSync.test.ts b/test/bun.js/sleepSync.test.ts new file mode 100644 index 000000000..dd2e8818a --- /dev/null +++ b/test/bun.js/sleepSync.test.ts @@ -0,0 +1,32 @@ +import { it, expect } from "bun:test"; +import { sleepSync } from "bun"; + +it("sleepSync uses milliseconds", async () => { + const start = Date.now(); + sleepSync(5); + const end = Date.now(); + expect(end - start).toBeGreaterThanOrEqual(5); + expect(end - start).toBeLessThan(10); +}); + +it("sleepSync with no arguments throws", async () => { + expect(() => sleepSync()).toThrow(); +}); + +it("sleepSync with non-numbers throws", async () => { + expect(() => sleepSync(true)).toThrow(); + expect(() => sleepSync(false)).toThrow(); + expect(() => sleepSync("hi")).toThrow(); + expect(() => sleepSync({})).toThrow(); + expect(() => sleepSync([])).toThrow(); + expect(() => sleepSync(undefined)).toThrow(); + expect(() => sleepSync(null)).toThrow(); +}); + +it("sleepSync with negative number throws", async () => { + expect(() => sleepSync(-10)).toThrow(); +}); + +it("can map with sleepSync", async () => { + [1, 2, 3].map(sleepSync); +}); |