-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
77 lines (62 loc) · 2.13 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
var path = require('path');
var fs = require('fs');
var funMap = require('fun-map');
var converter = require('./resultConverter');
function writeOutput(config, output, helper, logger) {
var log = logger.create('karma-json-result-reporter');
if (config.outputFile) {
helper.mkdirIfNotExists(path.dirname(config.outputFile), function() {
if (config.isSynchronous) {
log.debug('Writing test results to JSON file ' + config.outputFile);
try {
fs.writeFileSync(config.outputFile, JSON.stringify(output, null, 4));
} catch (err) {
log.warn('Cannot write test results to JSON file\n\t' + err.message);
}
} else {
fs.writeFile(config.outputFile, JSON.stringify(output, null, 4), function(err) {
if (err) {
log.warn('Cannot write test results to JSON file\n\t' + err.message);
} else {
log.debug('Test results were written to JSON file ' + config.outputFile);
}
});
}
});
} else {
process.stdout.write(JSON.stringify(output));
}
}
var JsonResultReporter = function(baseReporterDecorator, formatError, config, helper, logger) {
baseReporterDecorator(this);
var logMessageFormater = function(error) {
return formatError(error)
};
this.clear = function() {
this.results = [];
this.errors = [];
};
this.onBrowserError = function(browser, error) {
this.errors.push(error);
};
this.onSpecComplete = function(browser, result) {
result.log = result.log.map(logMessageFormater);
this.results.push(result);
};
this.onRunComplete = function() {
var output = {};
if (this.errors.length) {
output = converter.convertErrors(this.errors.map(logMessageFormater));
}
if (this.results.length) {
output = funMap.merge(output, converter.convertResults(this.results));
}
writeOutput(config, output, helper, logger);
this.clear();
};
this.clear();
};
JsonResultReporter.$inject = ['baseReporterDecorator', 'formatError', 'config.jsonResultReporter', 'helper', 'logger'];
module.exports = {
'reporter:json-result': ['type', JsonResultReporter]
};