/
reporter.js
139 lines (114 loc) · 3.42 KB
/
reporter.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const Bluebird = require('bluebird');
const log = require('npmlog');
const reporters = require('../reporters');
const isa = require('./isa');
const ReportFile = require('./report-file');
function setupReporter(name, out, config, app) {
let reporter;
if (isa(name, String)) {
let TestReporter = reporters[name];
if (TestReporter) {
reporter = new TestReporter(false, out, config, app);
}
} else if (isa(name, Function)) {
// name is a constructor function, ignore new-cap and instantiate
// eslint-disable-next-line new-cap
reporter = new name(false, out, config, app);
} else {
reporter = name;
}
if (!reporter) {
throw new Error('Test reporter `' + name + '` not found.');
}
return reporter;
}
class Reporter {
constructor(app, stdout, path) {
this.total = 0;
this.passed = 0;
this.skipped = 0;
this.todo = 0;
if (path) {
this.reportFile = new ReportFile(path);
}
let config = app.config;
if (path && config.get('xunit_intermediate_output') && config.get('reporter') === 'xunit') {
this.reporters = [
setupReporter('tap', stdout, config, app),
setupReporter(config.get('reporter'), this.reportFile.outputStream, config, app)
];
} else {
this.reporters = [setupReporter(config.get('reporter'), stdout, config, app)];
if (path) {
if (config.appMode === 'dev') {
let devModeFileReporter = config.get('dev_mode_file_reporter');
if (!devModeFileReporter) {
log.warn('You configured a `report_file`, you may want to configure the `dev_mode_file_reporter` as well. Using the `tap` logger now.');
devModeFileReporter = 'tap';
}
this.reporters.push(setupReporter(devModeFileReporter, this.reportFile.outputStream, config, app));
} else {
this.reporters.push(setupReporter(config.get('reporter'), this.reportFile.outputStream, config, app));
}
}
}
}
close() {
this.finish();
if (this.reportFile) {
return this.reportFile.close();
}
}
hasTests() {
return this.total > 0;
}
hasPassed() {
return this.total <= ((this.passed || 0) + (this.skipped || 0) + (this.todo || 0));
}
report(name, result) {
this.total++;
if (result.skipped) {
this.skipped++;
} else if (result.passed && !result.todo) {
this.passed++;
} else if (!result.passed && result.todo) {
this.todo++;
}
this.reporters.forEach(reporter => {
reporter.report(name, result);
});
}
}
Reporter.with = (app, stdout, path) => Bluebird.try(() => new Reporter(app, stdout, path)).disposer((reporter, promise) => {
if (promise.isRejected()) {
let err = promise.reason();
if (!err.hideFromReporter) {
reporter.report(null, {
passed: false,
name: err.name || 'unknown error',
error: {
message: err.message
}
});
}
}
return reporter.close();
});
function forwardToReporters(fn) {
return function() {
let args = new Array(arguments.length);
for (let i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
this.reporters.forEach(reporter => {
if (reporter[fn]) {
reporter[fn].apply(reporter, args);
}
});
};
}
['finish', 'onStart', 'onEnd', 'reportMetadata'].forEach(fn => {
Reporter.prototype[fn] = forwardToReporters(fn);
});
module.exports = Reporter;