-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
executable file
·186 lines (154 loc) · 3.99 KB
/
mod.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
interface Hooks {
beforeAll: Array<() => void | Promise<void>>;
beforeEach: Array<() => void | Promise<void>>;
afterEach: Array<() => void | Promise<void>>;
afterAll: Array<() => void | Promise<void>>;
waitingTests: number;
completedTests: number;
onlyTests: number;
completedOnlyTests: number;
}
interface StackItem extends Hooks {
name: string;
}
interface GlobalContext extends Hooks {
stack: StackItem[];
}
const globalContext: GlobalContext = {
stack: [],
beforeAll: [],
beforeEach: [],
afterEach: [],
afterAll: [],
waitingTests: 0,
completedTests: 0,
onlyTests: 0,
completedOnlyTests: 0,
};
function badArgs(): never {
throw new Error("Invalid test definition");
}
async function callAll(fns: Array<() => void | Promise<void>>): Promise<void> {
await Promise.all(fns.map((fn) => fn()));
}
/**
* Registers a test for `deno test` while including grouping and hooks.
*/
export function test(t: Deno.TestDefinition): void;
/**
* Registers a test for `deno test` while including grouping and hooks.
*/
export function test(name: string, fn: () => void | Promise<void>): void;
export function test(
t: Deno.TestDefinition | string,
testFn?: () => void | Promise<void>,
): void {
// Extract args
const { name: testName, fn, ...opts } = typeof t === "object"
? t
: (typeof testFn !== "undefined" ? { name: t, fn: testFn } : badArgs());
// Set up waiting count.
if (!opts.ignore) {
globalContext.waitingTests++;
globalContext.stack.map((name) => name.waitingTests++);
}
if (opts.only) {
globalContext.onlyTests++;
globalContext.stack.map((name) => name.onlyTests++);
}
// Generate name.
let name = globalContext.stack.map(({ name: n }) => n);
name.push(testName);
// Build hook stack.
const hooks: Hooks[] = [globalContext, ...globalContext.stack];
const revHooks: Hooks[] = [...hooks].reverse();
Deno.test({
name: name.join(" > "),
async fn() {
// Before.
for (const { beforeAll, beforeEach, completedTests } of hooks) {
if (completedTests === 0) {
await callAll(beforeAll);
}
await callAll(beforeEach);
}
// Test.
await fn();
for (const hook of hooks) {
hook.completedTests++;
if (opts.only) {
hook.completedOnlyTests++;
}
}
// After.
for (
const {
afterAll,
afterEach,
waitingTests,
completedTests,
onlyTests,
completedOnlyTests,
} of revHooks
) {
await callAll(afterEach);
if (
waitingTests === completedTests ||
(onlyTests > 0 && onlyTests === completedOnlyTests)
) {
await callAll(afterAll);
}
}
},
...opts,
});
}
/**
* Creates an environment in which all tests and hooks are grouped together.
*/
export function group(name: string, fn: () => void): void {
globalContext.stack.push({
name,
beforeAll: [],
beforeEach: [],
afterEach: [],
afterAll: [],
waitingTests: 0,
completedTests: 0,
onlyTests: 0,
completedOnlyTests: 0,
});
fn();
globalContext.stack.pop();
}
function getTopHooks(): Hooks {
if (globalContext.stack.length > 0) {
return globalContext.stack[globalContext.stack.length - 1];
} else {
return globalContext;
}
}
/**
* Adds a function to be called before all tests are run.
*/
export function beforeAll(fn: () => void | Promise<void>): void {
getTopHooks().beforeAll.push(fn);
}
/**
* Adds a function to be called before each test runs.
*/
export function beforeEach(fn: () => void | Promise<void>): void {
getTopHooks().beforeEach.push(fn);
}
/**
* Adds a function to be called after each test runs.
*/
export function afterEach(fn: () => void | Promise<void>): void {
getTopHooks().afterEach.push(fn);
}
/**
* Adds a function to be called after all tests have run.
*/
export function afterAll(fn: () => void | Promise<void>): void {
getTopHooks().afterAll.push(fn);
}