-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLIReporter.ts
More file actions
151 lines (113 loc) · 3.52 KB
/
CLIReporter.ts
File metadata and controls
151 lines (113 loc) · 3.52 KB
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
import { performance } from "perf_hooks";
import { GluegunToolbox } from "gluegun";
import {
TestResultState,
Reporter,
TestInTestFile,
TestResult,
ValidConfig
} from "@testingrequired/bespin-core";
export class CLIReporter extends Reporter {
private startTime: number;
private spinner: any;
constructor(private toolbox: GluegunToolbox) {
super();
}
onRuntimeStart(config: ValidConfig) {
const { print, meta } = this.toolbox;
const { bold, underline, italic } = print.colors;
print.divider();
print.info(`${bold(italic("bespin"))} v${meta.version()}`);
print.divider();
print.info(`${underline("Config")}\n`);
print.table(
[
["Field", "Value"],
["Locator", config.locator.constructor.name],
["Parser", config.parser.constructor.name],
["Runner", config.runner.constructor.name],
["Reporters", config.reporters.map(x => x.constructor.name).join(", ")]
],
{
format: "markdown"
}
);
print.divider();
this.startTime = performance.now();
this.spinner = print.spin("Loading tests...");
}
onRunStart(testsInTestFiles: Array<TestInTestFile>) {
this.spinner.stop();
const { print } = this.toolbox;
const { underline } = print.colors;
print.divider();
print.info(`${underline("Tests")}\n`);
const testFiles = Array.from(
new Set(testsInTestFiles.map(x => x.testFilePath))
);
print.table(
[
["Field", "Value"],
["Test Files", testFiles.length.toString()],
["Tests", testsInTestFiles.length.toString()]
],
{
format: "markdown"
}
);
print.divider();
this.spinner = print.spin("Running tests...");
}
onRunEnd(results: Array<[TestInTestFile, TestResult]>): void {
this.spinner.stop();
const { print } = this.toolbox;
const { bold, underline, italic } = print.colors;
print.info(`${underline("Results")}\n`);
const groups = groupBy(results, "testFilePath");
Object.entries(groups).forEach(entry => {
const [testFilePath, results] = entry;
print.info(testFilePath);
results.forEach(([testInTestFile, { state, time, message, error }]) => {
const formattedTime = `${time.toFixed(2)}ms`;
const printMessage = `- ${testInTestFile.testName} ${state} (${formattedTime})`;
if (state === TestResultState.PASS) {
print.success(printMessage);
} else {
print.error(
`${printMessage}\n\nMessage:\n\n${message ?? ""}\n${error}`
);
}
});
});
print.divider();
const passingRun = results
.map(([_, result]) => result)
.every(({ state }) => state === TestResultState.PASS);
if (passingRun) {
print.success(bold(italic("PASS")));
} else {
print.error(bold(italic(`FAIL`)));
}
const endTime = performance.now();
const time = (endTime - this.startTime) / 1000;
print.info(`Done in ${time.toFixed(2)}s`);
print.divider();
}
}
function groupBy(
arr: Array<[TestInTestFile, TestResult]>,
key: number | string
): Record<string, Array<[TestInTestFile, TestResult]>> {
return arr.reduce(function(
groups: Record<string, Array<[TestInTestFile, TestResult]>>,
item: [TestInTestFile, TestResult]
) {
const [testInTestFile] = item as [TestInTestFile, TestResult];
if (!Array.isArray(groups[testInTestFile[key]])) {
groups[testInTestFile[key]] = [];
}
groups[testInTestFile[key]].push(item);
return groups;
},
{});
}