-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
getPromiseAssertionApi.ts
83 lines (63 loc) 路 1.99 KB
/
getPromiseAssertionApi.ts
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
import {聽assert } from "https://deno.land/x/tsafe@v1.5.2/assert.ts";
import { sameFactory } from "../inDepth/same.ts";
export function getPromiseAssertionApi(
params?: { takeIntoAccountArraysOrdering: boolean; }
) {
const is = !!params ? sameFactory(params).same : (<T>(o1: T, o2: T) => o1 === o2);
function mustResolve<T>(
params: {
promise: Promise<T>;
expectedData?: T;
delay?: number
}
): Promise<T> {
const timer = setTimeout(
() => assert(false, "did not resolve in time"),
params.delay !== undefined ? params.delay : 0
);
return params.promise.then(data => {
clearTimeout(timer);
if (!("expectedData" in params)) {
return data;
}
assert(is(data, params.expectedData), "Not equals expected value");
return data;
});
}
/** Must reject within delay ms*/
function mustReject(
params: {
promise: Promise<any>,
expectedRejectedValue?: any,
delay?: number
}
) {
const timer = setTimeout(
() => assert(false, "did not reject in time"),
params.delay !== undefined ? params.delay : 0
);
return params.promise.then(
() => assert(false, "resolved"),
error => {
clearTimeout(timer);
if ("expectedRejectedValue" in params) {
assert(
is(
error,
params.expectedRejectedValue
)
);
}
}
);
}
function mustStayPending(p: Promise<any>): void {
p
.then(
() => assert(false, `Has fulfilled`),
() => assert(false, `has rejected`)
)
;
}
return { mustResolve, mustReject, mustStayPending };
}