This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
63 lines (52 loc) · 1.58 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
/* jshint node: true */
'use strict'
var path = require('path')
var fs = require('fs')
//
var JSONReporter = function (baseReporterDecorator, config, helper, logger) {
var log = logger.create('karma-json-reporter')
baseReporterDecorator(this)
var history = {
browsers: {},
result: {},
summary: {}
}
var reporterConfig = config.jsonReporter || {}
var stdout =
typeof reporterConfig.stdout !== 'undefined' ? 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
var json = JSON.stringify(history, undefined, '\t')
if (stdout) {
process.stdout.write(json)
}
if (outputFile) {
helper.mkdirIfNotExists(path.dirname(outputFile), function () {
fs.writeFile(outputFile, json, function (err) {
if (err) {
log.warn('Cannot write JSON\n\t' + err.message)
} else {
log.debug('JSON written to "%s".', outputFile)
}
})
})
} else {
history.result = {}
}
}
}
JSONReporter.$inject = ['baseReporterDecorator', 'config', 'helper', 'logger']
// PUBLISH DI MODULE
module.exports = {
'reporter:json': ['type', JSONReporter]
}