forked from danthareja/karma-js-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (70 loc) · 1.88 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
'use strict'
/**
* jsReporter
*
* Collect stats of a test run and pass them to a callback
*
* Example Usage (in karma.conf.js):
*
* reporters: ['js'],
*
* jsReporter: function(testResults) {
* // Do something with tests results here
* }
*/
function jsReporter(baseReporterDecorator, formatError, config) {
// The function that gets called after files are run
// Defaults to process.stdout.write
var done = config || function(output){
process.stdout.write(JSON.stringify(output, null, 2));
};
// Private variables to hold different test buckets
var tests = [],
pending = [],
failures = [],
successes = [];
// Extend the base reporter
baseReporterDecorator(this);
this.specSuccess = function(browser, result) {
successes.push(result);
};
this.specFailure = function(browser, result) {
failures.push(result);
};
this.specSkipped = function(browser, result) {
pending.push(result);
};
this.onRunComplete = function(browsers, results) {
var output = {
stats: results,
pending: pending.map(clean),
failures: failures.map(clean),
successes: successes.map(clean)
};
// Tests are all done!
// pass them to our callback defined in config.jsReporter
done(output);
};
function clean(test) {
var cleaned = {
title: test.description,
description: test.suite.join(' ').concat(' ' + test.description),
duration: test.time
}
if (test.log[0]) cleaned.err = errorToJSON(test.log[0]);
return cleaned;
}
function errorToJSON(error) {
var formatted = formatError(error).split('\n');
return {
message: formatted[0],
stack: formatted[1].trim()
}
}
}
// Inject dependencies
jsReporter.$inject = ['baseReporterDecorator', 'formatError', 'config.jsReporter'];
// PUBLISH DI MODULE
module.exports = {
'reporter:js': ['type', jsReporter]
};