-
Notifications
You must be signed in to change notification settings - Fork 110
/
reporter.ts
100 lines (69 loc) · 3.45 KB
/
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
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
import * as istanbulCoverage from "istanbul-lib-coverage";
import * as istanbulReport from "istanbul-lib-report";
import * as istanbulSourceMaps from "istanbul-lib-source-maps";
import * as istanbulReports from "istanbul-reports";
import * as lodash from "lodash";
import * as path from "path";
import { Logger } from "log4js";
import { Threshold } from "../istanbul/threshold";
import { Configuration } from "../shared/configuration";
const reporterName = "karma-typescript";
export class Reporter {
public create: (baseReporterDecorator: any, logger: any) => void;
private log: Logger;
private coverageMap: WeakMap<any, any>;
constructor(config: Configuration, threshold: Threshold) {
const that = this;
// tslint:disable-next-line:only-arrow-functions
this.create = function(baseReporterDecorator: any, logger: any) {
baseReporterDecorator(that);
that.log = logger.create(`reporter.${reporterName}`);
this.onRunStart = () => {
that.coverageMap = new WeakMap<any, any>();
};
this.onBrowserComplete = (browser: any, result: any) => {
if (result && result.coverage) {
that.coverageMap.set(browser, result.coverage);
}
};
this.onRunComplete = (browsers: any[], results: any) => {
browsers.forEach((browser: any) => {
const coverage = that.coverageMap.get(browser);
const coverageMap = istanbulCoverage.createCoverageMap();
coverageMap.merge(coverage);
const sourceMapStore = istanbulSourceMaps.createSourceMapStore();
const remappedCoverageMap = sourceMapStore.transformCoverage(coverageMap).map;
const tree = istanbulReport.summarizers.pkg(remappedCoverageMap);
if (results && config.hasCoverageThreshold && !threshold.check(browser, remappedCoverageMap)) {
results.exitCode = 1;
}
Object.keys(config.reports).forEach((reportType: any) => {
const destination = that.getReportDestination(browser, config.reports, reportType);
if (destination) {
that.log.debug("Writing coverage to %s", destination);
}
const context = istanbulReport.createContext( { dir: destination } );
tree.visit(istanbulReports.create(reportType), context);
});
});
};
};
Object.assign(this.create, { $inject: ["baseReporterDecorator", "logger", "config"] });
}
private getReportDestination(browser: any, reports: any, reportType: any) {
const reportConfig = reports[reportType];
if (lodash.isPlainObject(reportConfig)) {
let subdirectory = reportConfig.subdirectory || browser.name;
if (typeof subdirectory === "function") {
subdirectory = subdirectory(browser);
}
return path.join(reportConfig.directory || "coverage",
subdirectory,
reportConfig.filename || reportType);
}
if (lodash.isString(reportConfig) && reportConfig.length > 0) {
return path.join(reportConfig, browser.name, reportType);
}
return null;
}
}