-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpectations.ts
More file actions
50 lines (45 loc) · 1.22 KB
/
expectations.ts
File metadata and controls
50 lines (45 loc) · 1.22 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
import {Fail} from "./errors.js"
import {deepEqual} from "./deep-equal.js"
export const makeExpectations = (a: any) => ({
ok: () => !!a,
available: () => a !== undefined && a !== null,
nullish: () => a === undefined || a === null,
happy: () => a !== undefined && a !== null,
sad: () => a === undefined || a === null,
is: (b: any) => a === b,
isnt: (b: any) => a !== b,
gt: (b: any) => a > b,
gte: (b: any) => a >= b,
lt: (b: any) => a < b,
lte: (b: any) => a <= b,
deep: (b: any) => deepEqual(a, b),
throws: (ErrorClass?: new(...a: any[]) => any) => {
try {
if (typeof a !== "function")
throw new Fail(".throws() requires a function")
a()
return false
}
catch (error) {
if (ErrorClass && !(error instanceof ErrorClass))
return false
return true
}
},
throwsAsync: async(ErrorClass?: new(...params: any[]) => any) => {
try {
if (typeof a !== "function")
throw new Fail(".throwsAsync() requires an async function")
const promise = a()
if (!(promise instanceof Promise))
throw new Fail(".throwsAsync() requires an *async* function")
await promise
return false
}
catch (error) {
if (ErrorClass && !(error instanceof ErrorClass))
return false
return true
}
},
})