-
Notifications
You must be signed in to change notification settings - Fork 39
/
test_runner.ts
211 lines (197 loc) · 5.54 KB
/
test_runner.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
export interface Chalk {
green(text: string): string;
red(text: string): string;
gray(text: string): string;
}
export interface NodeProcess {
stdout: {
write(text: string): void;
};
exit(code: number): number;
}
export interface RunTestDefinitionsOptions {
chalk: Chalk;
process: NodeProcess;
/** The file the tests are running in. */
origin: string;
}
export interface TestDefinition {
name: string;
fn: (context: TestContext) => Promise<void> | void;
ignore?: boolean;
}
export interface TestContext {
name: string;
parent: TestContext | undefined;
origin: string;
err: any;
children: TestContext[];
hasFailingChild: boolean;
getOutput(): string;
step(
nameOrDefinition: string | TestDefinition,
fn?: (context: TestContext) => void | Promise<void>,
): Promise<boolean>;
status: "ok" | "fail" | "pending" | "ignored";
}
export async function runTestDefinitions(
testDefinitions: TestDefinition[],
options: RunTestDefinitionsOptions,
) {
const testFailures = [];
for (const definition of testDefinitions) {
options.process.stdout.write("test " + definition.name + " ...");
if (definition.ignore) {
options.process.stdout.write(` ${options.chalk.gray("ignored")}\n`);
continue;
}
const context = getTestContext(definition, undefined);
let pass = false;
try {
await definition.fn(context);
if (context.hasFailingChild) {
testFailures.push({
name: definition.name,
err: new Error("Had failing test step."),
});
} else {
pass = true;
}
} catch (err) {
testFailures.push({ name: definition.name, err });
}
const testStepOutput = context.getOutput();
if (testStepOutput.length > 0) {
options.process.stdout.write(testStepOutput);
} else {
options.process.stdout.write(" ");
}
options.process.stdout.write(getStatusText(pass ? "ok" : "fail"));
options.process.stdout.write("\n");
}
if (testFailures.length > 0) {
options.process.stdout.write("\nFAILURES");
for (const failure of testFailures) {
options.process.stdout.write("\n\n");
options.process.stdout.write(failure.name + "\n");
options.process.stdout.write(
indentText((failure.err?.stack ?? failure.err).toString(), 1),
);
}
options.process.exit(1);
}
function getTestContext(
definition: TestDefinition,
parent: TestContext | undefined,
): TestContext {
return {
name: definition.name,
parent,
origin: options.origin,
/** @type {any} */
err: undefined,
status: "ok",
children: [],
get hasFailingChild() {
return this.children.some((c) =>
c.status === "fail" || c.status === "pending"
);
},
getOutput() {
let output = "";
if (this.parent) {
output += "test " + this.name + " ...";
}
if (this.children.length > 0) {
output += "\n" + this.children.map((c) =>
indentText(c.getOutput(), 1)
).join("\n") + "\n";
} else if (!this.err) {
output += " ";
}
if (this.parent && this.err) {
output += "\n";
}
if (this.err) {
output += indentText((this.err.stack ?? this.err).toString(), 1);
if (this.parent) {
output += "\n";
}
}
if (this.parent) {
output += getStatusText(this.status);
}
return output;
},
async step(nameOrTestDefinition, fn) {
const definition = getDefinition();
const context = getTestContext(definition, this);
context.status = "pending";
this.children.push(context);
if (definition.ignore) {
context.status = "ignored";
return false;
}
try {
await definition.fn(context);
context.status = "ok";
if (context.hasFailingChild) {
context.status = "fail";
return false;
}
return true;
} catch (err) {
context.status = "fail";
context.err = err;
return false;
}
/** @returns {TestDefinition} */
function getDefinition() {
if (typeof nameOrTestDefinition === "string") {
if (!(fn instanceof Function)) {
throw new TypeError("Expected function for second argument.");
}
return {
name: nameOrTestDefinition,
fn,
};
} else if (typeof nameOrTestDefinition === "object") {
return nameOrTestDefinition;
} else {
throw new TypeError(
"Expected a test definition or name and function.",
);
}
}
},
};
}
function getStatusText(status: TestContext["status"]) {
switch (status) {
case "ok":
return options.chalk.green(status);
case "fail":
case "pending":
return options.chalk.red(status);
case "ignored":
return options.chalk.gray(status);
default: {
const _assertNever: never = status;
return status;
}
}
}
function indentText(text: string, indentLevel: number) {
if (text === undefined) {
text = "[undefined]";
} else if (text === null) {
text = "[null]";
} else {
text = text.toString();
}
return text.split(/\r?\n/)
.map((line) => " ".repeat(indentLevel) + line)
.join("\n");
}
}