forked from douglasduteil/karma-json-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (42 loc) · 1.41 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
/* jshint node: true */
'use strict';
var path = require('path');
var fs = require('fs');
//
var JSONReporter = function (baseReporterDecorator, config, helper) {
baseReporterDecorator(this);
var history = {
browsers : {},
result : {},
summary : {}
};
var reporterConfig = config.jsonReporter || {};
var stdout = reporterConfig.stdout || true;
var outputFile = (reporterConfig.outputFile) ? helper.normalizeWinPath(path.resolve(config.basePath, reporterConfig.outputFile )) : null;
this.onSpecComplete = function(browser, result) {
history.result[browser.id] = history.result[browser.id] || [];
history.result[browser.id].push(result);
history.browsers[browser.id] = history.browsers[browser.id] || browser;
};
this.onRunComplete = function(browser, result) {
history.summary = result;
if(stdout) process.stdout.write(JSON.stringify(history));
if(outputFile) {
helper.mkdirIfNotExists(path.dirname(outputFile), function() {
fs.writeFile(outputFile, JSON.stringify(history), function(err) {
if (err) {
log.warn('Cannot write JSON\n\t' + err.message);
} else {
log.debug('JSON written to "%s".', outputFile);
}
});
});
}
history.result = {};
};
};
JSONReporter.$inject = ['baseReporterDecorator','config','helper'];
// PUBLISH DI MODULE
module.exports = {
'reporter:json': ['type', JSONReporter]
};