diff options
author | 2022-10-24 04:10:44 -0700 | |
---|---|---|
committer | 2022-10-24 04:10:44 -0700 | |
commit | 0b0db78799308cc3de148959b9496901d90b0794 (patch) | |
tree | 161cf1c396c6e1534318c73bd5ce3e1c1ca787cf /test | |
parent | b3434a8b883ee98324cea2c7d14dd988e6bb8adc (diff) | |
download | bun-0b0db78799308cc3de148959b9496901d90b0794.tar.gz bun-0b0db78799308cc3de148959b9496901d90b0794.tar.zst bun-0b0db78799308cc3de148959b9496901d90b0794.zip |
`Bun.peek`
Diffstat (limited to 'test')
-rw-r--r-- | test/bun.js/peek.test.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/bun.js/peek.test.ts b/test/bun.js/peek.test.ts new file mode 100644 index 000000000..3b2237291 --- /dev/null +++ b/test/bun.js/peek.test.ts @@ -0,0 +1,40 @@ +import { peek } from "bun"; +import { expect, test } from "bun:test"; + +test("peek", () => { + const promise = Promise.resolve(true); + + // no await necessary! + expect(peek(promise)).toBe(true); + + // if we peek again, it returns the same value + const again = peek(promise); + expect(again).toBe(true); + + // if we peek a non-promise, it returns the value + const value = peek(42); + expect(value).toBe(42); + + // if we peek a pending promise, it returns the promise again + const pending = new Promise(() => {}); + expect(peek(pending)).toBe(pending); + + // If we peek a rejected promise, it: + // - returns the error + // - does not mark the promise as handled + const rejected = Promise.reject( + new Error("Succesfully tested promise rejection") + ); + expect(peek(rejected).message).toBe("Succesfully tested promise rejection"); +}); + +test("peek.status", () => { + const promise = Promise.resolve(true); + expect(peek.status(promise)).toBe("fulfilled"); + + const pending = new Promise(() => {}); + expect(peek.status(pending)).toBe("pending"); + + const rejected = Promise.reject(new Error("oh nooo")); + expect(peek.status(rejected)).toBe("rejected"); +}); |