-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
189 lines (109 loc) · 4.08 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var mochaReporters = require('mocha/lib/reporters');
var EventEmitter = require('events').EventEmitter;
var getMochaReporter = function(reporterName) {
var MochaReporter = mochaReporters[reporterName];
if (!MochaReporter) {
try {
MochaReporter = require(reporterName);
} catch (err) {
if (err.message.indexOf('Cannot find module') >= 0) {
console.warn('"' + reporterName + '" reporter not found');
} else {
console.warn('"' + reporterName + '" reporter blew up with error:\n' + err.stack);
}
}
}
if (!MochaReporter && reporterName === 'teamcity') {
console.warn('The Teamcity reporter was moved to a package named ' +
'mocha-teamcity-reporter ' +
'(https://npmjs.org/package/mocha-teamcity-reporter).');
}
if (!MochaReporter) throw new Error('invalid reporter "' + reporterName + '"');
return MochaReporter;
};
var Reporter = function(baseReporterDecorator, formatError, reportSlowerThan, pluginConfig) {
baseReporterDecorator(this);
var reporterName = (pluginConfig && pluginConfig.reporter) || 'spec';
var MochaReporter = getMochaReporter(reporterName);
var createMochaTestResult = function(result) {
return {
title: result.description,
fullTitle: function() {
return result.suite.concat(result.description).join(' ');
},
titlePath: function() {
return [result.suite, result.description];
},
duration: result.time,
slow: function() {
return reportSlowerThan;
}
};
};
var createMochaErrors = function(result) {
return result.log.map(function(log, k) {
var formattedStack = formatError(log);
if (result.assertionErrors && result.assertionErrors[k]) {
var err = result.assertionErrors[k];
err.stack = formattedStack;
return err;
} else {
return {
message: formattedStack
};
}
});
};
var runner;
this.onRunStart = function(browsers) {
runner = new EventEmitter();
var mochaReporter = new MochaReporter(runner); // eslint-disable-line no-unused-vars
runner.emit('start');
runner.emit('suite', {
title: '',
root: true
});
};
this.onBrowserStart = function(browser) {
};
var lastSuites = [];
this.onSpecComplete = function(browser, result) {
var suites = result.suite;
var maxCommon = 0;
for (var i = 0, n = Math.min(lastSuites.length, suites.length); i < n; i++) {
if (lastSuites[i] === suites[i]) maxCommon = i + 1;
else break;
}
lastSuites.slice(maxCommon).reverse().forEach(function(suiteName) {
runner.emit('suite end');
});
suites.slice(maxCommon).forEach(function(suiteName) {
runner.emit('suite', {
title: suiteName,
root: false
});
});
lastSuites = suites;
var mochaTestResult = createMochaTestResult(result);
if (result.success) runner.emit('pass', mochaTestResult);
else if (result.skipped) runner.emit('pending', mochaTestResult);
else {
createMochaErrors(result).forEach(function(err) {
runner.emit('fail', mochaTestResult, err);
});
}
};
this.onRunComplete = function(browsers, results) {
var revLastSuites = lastSuites.slice().reverse();
lastSuites = [];
revLastSuites.forEach(function(suiteName, k) {
runner.emit('suite end');
});
runner.emit('suite end');
runner.emit('end');
};
};
Reporter.$inject = ['baseReporterDecorator', 'formatError', 'config.reportSlowerThan', 'config.mochaOwnReporter'];
module.exports = {
'reporter:mocha-own': ['type', Reporter]
};