-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.ts
More file actions
91 lines (74 loc) · 2.59 KB
/
utils.test.ts
File metadata and controls
91 lines (74 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { describe, expect, it } from "bun:test"
import {
CancellationError,
Panic,
RetryExhaustedError,
TimeoutError,
UnhandledException,
} from "../errors"
import { assertUnreachable, checkIsControlError, checkIsPromiseLike, invariant } from "../utils"
describe("checkIsControlError", () => {
it("returns true for CancellationError", () => {
expect(checkIsControlError(new CancellationError())).toBe(true)
})
it("returns true for Panic", () => {
expect(checkIsControlError(new Panic("RUN_CATCH_HANDLER_THROW"))).toBe(true)
})
it("returns true for TimeoutError", () => {
expect(checkIsControlError(new TimeoutError())).toBe(true)
})
it("returns false for RetryExhaustedError", () => {
expect(checkIsControlError(new RetryExhaustedError())).toBe(false)
})
it("returns false for UnhandledException", () => {
expect(checkIsControlError(new UnhandledException())).toBe(false)
})
it("returns false for plain Error", () => {
expect(checkIsControlError(new Error("boom"))).toBe(false)
})
it("returns false for non-error values", () => {
expect(checkIsControlError("string")).toBe(false)
expect(checkIsControlError(null)).toBe(false)
})
})
describe("checkIsPromiseLike", () => {
it("returns true for native Promise", () => {
expect(checkIsPromiseLike(Promise.resolve("ok"))).toBe(true)
})
it("returns true for thenable objects", () => {
const thenable: Record<string, unknown> = {}
Reflect.set(thenable, "then", (_resolve: (value: string) => void) => null)
expect(checkIsPromiseLike(thenable)).toBe(true)
})
it("returns false for non-thenable values", () => {
expect(checkIsPromiseLike(null)).toBe(false)
expect(checkIsPromiseLike(123)).toBe(false)
expect(checkIsPromiseLike({})).toBe(false)
})
})
describe("invariant", () => {
it("does nothing when the condition is truthy", () => {
expect(() => {
invariant(true, new Error("boom"))
}).not.toThrow()
})
it("throws the provided error when the condition is falsy", () => {
const error = new Panic("RUN_SYNC_TRY_PROMISE")
expect(() => {
invariant(false, error)
}).toThrow(error)
})
})
describe("assertUnreachable", () => {
it("throws with the unreachable value", () => {
let error: unknown
try {
assertUnreachable("unexpected" as never, "UNREACHABLE_RETRY_POLICY_BACKOFF")
} catch (caughtError) {
error = caughtError
}
expect(error).toBeInstanceOf(Panic)
expect((error as Panic).code).toBe("UNREACHABLE_RETRY_POLICY_BACKOFF")
expect((error as Error).message).toBe("Unreachable case: unexpected")
})
})