forked from as-pect/as-pect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSONReporter.ts
83 lines (76 loc) 路 2.28 KB
/
JSONReporter.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
import { TestReporter } from "../test/TestReporter";
import { TestContext } from "../test/TestContext";
import { WriteStream, createWriteStream } from "fs";
import { basename, extname, dirname, join } from "path";
import { TestGroup } from "../test/TestGroup";
import { TestResult } from "../test/TestResult";
/**
* This class reports all relevant test statistics to a JSON file located at
* `{testLocation}.spec.json`.
*/
export default class JSONReporter extends TestReporter {
constructor(_options?: any) {
super();
}
protected file: WriteStream | null = null;
private first: boolean = true;
public onStart(suite: TestContext): void {
const extension = extname(suite.fileName);
const dir = dirname(suite.fileName);
const base = basename(suite.fileName, extension);
const outPath = join(process.cwd(), dir, base + ".json");
this.file = createWriteStream(outPath, "utf8");
this.file.write("[");
this.first = true;
}
public onGroupStart(): void {}
public onGroupFinish(): void {}
public onFinish(): void {
this.file!.end("\n]");
}
public onTestStart() {}
public onTestFinish(group: TestGroup, result: TestResult) {
this.file!.write(
(this.first ? "\n" : ",\n") +
JSON.stringify({
group: group.name,
name: result.name,
ran: result.ran,
pass: result.pass,
runtime: result.runTime,
message: result.message,
actual: result.actual ? result.actual.message : null,
expected: result.expected ? result.expected.message : null,
average: result.average,
median: result.median,
max: result.max,
min: result.min,
stdDev: result.stdDev,
variance: result.variance,
}),
);
this.first = false;
}
public onTodo(group: TestGroup, desc: string) {
this.file!.write(
(this.first ? "\n" : ",\n") +
JSON.stringify({
group: group.name,
name: "TODO:" + desc,
ran: false,
pass: null,
runtime: 0,
message: "",
actual: null,
expected: null,
average: 0,
median: 0,
max: 0,
min: 0,
stdDev: 0,
variance: 0,
}),
);
this.first = false;
}
}