-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
96 lines (70 loc) · 2.62 KB
/
index.js
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
'use strict';
const istanbul = require('istanbul');
const remap = require('remap-istanbul/lib/remap');
const writeReport = require('remap-istanbul/lib/writeReport');
function KarmaRemapIstanbul(baseReporterDecorator, logger, config) {
baseReporterDecorator(this);
const log = logger.create('reporter.remap-istanbul');
const remapIstanbulReporterConfig = config.remapIstanbulReporter || {};
const reports = remapIstanbulReporterConfig.reports || {};
const remapOptions = remapIstanbulReporterConfig.remapOptions || {};
const reportOptions = remapIstanbulReporterConfig.reportOptions || {};
const coverageMap = new WeakMap();
const baseReporterOnRunStart = this.onRunStart;
this.onRunStart = function () {
baseReporterOnRunStart.apply(this, arguments);
};
this.onBrowserComplete = function (browser, result) {
if (!result || !result.coverage) {
return;
}
coverageMap.set(browser, result.coverage);
};
let reportFinished = () => {};
const baseReporterOnRunComplete = this.onRunComplete;
this.onRunComplete = function (browsers) {
baseReporterOnRunComplete.apply(this, arguments);
// Collect the unmapped coverage information for all browsers in this run
const unmappedCoverage = (() => {
const collector = new istanbul.Collector();
browsers.forEach(browser => {
const coverage = coverageMap.get(browser);
coverageMap.delete(browser);
if (!coverage) {
return;
}
collector.add(coverage);
});
return collector.getFinalCoverage();
})();
let sourceStore = istanbul.Store.create('memory');
const collector = remap(unmappedCoverage, Object.assign({
sources: sourceStore
}, remapOptions));
if (Object.keys(sourceStore.map).length === 0) {
sourceStore = undefined;
}
Promise.all(Object.keys(reports).map(reportType => {
const destination = reports[reportType];
log.debug('Writing coverage to %s', destination);
return writeReport(collector, reportType, reportOptions, destination, sourceStore);
})).catch(err => {
log.error(err);
}).then(() => {
collector.dispose();
reportFinished();
// Reassign `onExit` to just call `done` since all reports have been written
this.onExit = function (done) {
done();
};
});
};
this.onExit = function (done) {
// Reports have not been written, so assign `done` to `reportFinished`
reportFinished = done;
};
}
KarmaRemapIstanbul.$inject = ['baseReporterDecorator', 'logger', 'config'];
module.exports = {
'reporter:karma-remap-istanbul': ['type', KarmaRemapIstanbul]
};