-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
utils.ts
63 lines (56 loc) · 1.21 KB
/
utils.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
/**
* Test timeout.
*/
export const TEST_TIMEOUT = 3000;
/**
* A no-op _describe_ method.
*
* @param name
* @param fn
*/
export async function describe(_name: string, fn: () => void | Promise<void>) {
fn();
}
/**
* An _it_ wrapper around `Deno.test`.
*
* @param name
* @param fn
*/
export async function it(
name: string,
fn: (done?: any) => void | Promise<void>,
) {
Deno.test(name, async () => {
let done: any = (err?: any) => {
if (err) throw err;
};
let race: Promise<unknown> = Promise.resolve();
if (fn.length === 1) {
let resolve: () => void;
const donePromise = new Promise((r) => {
resolve = r;
});
let timeoutId: number;
race = Promise.race([
new Promise((_, reject) =>
timeoutId = setTimeout(() => {
reject(
new Error(
`test "${name}" failed to complete by calling "done" within ${TEST_TIMEOUT}ms.`,
),
);
}, TEST_TIMEOUT)
),
donePromise,
]);
done = (err?: any) => {
clearTimeout(timeoutId);
resolve();
if (err) throw err;
};
}
await fn(done);
await race;
});
}