-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
test_util.ts
50 lines (49 loc) 路 1.44 KB
/
test_util.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
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import {
test,
TestFunction
} from "./vendor/https/deno.land/std/testing/mod.ts";
export type SetupFunc = () => any | Promise<any>;
export interface Testing {
run(desc: string, body: TestFunction): void;
beforeAfterAll(func: () => SetupFunc | Promise<SetupFunc>): void;
beforeAfterEach(func: () => SetupFunc | Promise<SetupFunc>): void;
}
export function it(desc: string, func: (t: Testing) => void) {
let testCnt = 0;
let beforeAllFunc: SetupFunc | undefined;
let afterAllFunc: SetupFunc | undefined;
let beforeEachFunc: SetupFunc | undefined;
let afterEachFunc: SetupFunc | undefined;
function beforeAfterAll(func) {
beforeAllFunc = func;
}
function beforeAfterEach(func) {
beforeEachFunc = func;
}
function run(desc2: string, func2: () => any | Promise<any>) {
test(`${desc} ${desc2}`, async () => {
if (testCnt === 0 && beforeAllFunc) {
afterAllFunc = await beforeAllFunc();
}
testCnt++;
try {
if (beforeEachFunc) {
afterEachFunc = await beforeEachFunc();
}
await func2();
} finally {
if (afterEachFunc) {
await afterEachFunc();
}
setTimeout(async () => {
testCnt--;
if (testCnt === 0 && afterAllFunc) {
await afterAllFunc();
}
}, 0);
}
});
}
func({ beforeAfterAll, beforeAfterEach, run });
}