forked from Klaveness-Digital/cypress-parallel
-
Notifications
You must be signed in to change notification settings - Fork 6
/
knapsack-reporter.ts
53 lines (43 loc) · 1.17 KB
/
knapsack-reporter.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
import fs from "fs";
import Mocha from "mocha";
import { createError } from "./lib/assertions";
const { EVENT_RUN_END, EVENT_SUITE_BEGIN } = Mocha.Runner.constants;
export = class KnapsackReporter {
constructor(runner: any, options: any) {
const stats = runner.stats;
const { reporterOptions } = options;
const { output } = reporterOptions ? reporterOptions : { output: null };
if (!output) {
throw createError(
"'output' must be configured for KnapsackReporter to work"
);
}
let spec: any;
runner
.on(EVENT_SUITE_BEGIN, (suite: any) => {
if (suite.root) {
spec = suite.file;
}
})
.once(EVENT_RUN_END, () => {
if (!spec) {
throw createError("'spec' hasn't been determined");
}
const { duration } = stats;
const content = fs.existsSync(output)
? JSON.parse(fs.readFileSync(output).toString())
: {};
fs.writeFileSync(
output,
JSON.stringify(
{
...content,
[spec]: duration,
},
null,
2
)
);
});
}
};