Skip to content

Commit e0c829c

Browse files
committed
MochaReporter: update Mocha reporter based on js-reporters adapter.
1 parent 1dcb228 commit e0c829c

File tree

2 files changed

+23
-65
lines changed

2 files changed

+23
-65
lines changed

lib/_patch/mocha-plugin.js

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,32 @@
11
(function() {
2-
function stack(err) {
3-
var str = err.stack || err.toString();
2+
var runner = new JsReporters.MochaAdapter(mocha);
3+
var tracebacks = [];
4+
var total = 0,
5+
passed = 0,
6+
failed = 0;
47

5-
if (!~str.indexOf(err.message)) {
6-
str = err.message + '\n' + str;
7-
}
8+
runner.on('testEnd', function(test) {
9+
total = total + 1
810

9-
if ('[object Error]' == str) {
10-
str = err.message;
11-
}
11+
passed = passed + (test.status === 'passed' ? 1 : 0);
12+
failed = failed + (test.status === 'failed' ? 1 : 0);
1213

13-
if (!err.stack && err.sourceURL && err.line !== undefined) {
14-
str += '\n(' + err.sourceURL + ':' + err.line + ')';
15-
}
16-
return str.replace(/^/gm, ' ');
17-
}
18-
19-
function title(test) {
20-
return test.fullTitle().replace(/#/g, '');
21-
}
22-
23-
var origReporter = mocha._reporter;
24-
25-
Mocha.BrowserStack = function(runner, root) {
26-
origReporter.apply(this, arguments);
27-
28-
var count = 1,
29-
that = this,
30-
failures = 0,
31-
passes = 0,
32-
start = 0,
33-
tracebacks = [];
34-
35-
runner.on('start', function() {
36-
start = (new Date).getTime();
37-
});
38-
39-
runner.on('test end', function(test) {
40-
count += 1;
14+
test.errors.forEach(function(error) {
15+
tracebacks.push(error)
4116
});
17+
});
4218

43-
runner.on('pass', function(test) {
44-
passes += 1;
45-
});
46-
47-
runner.on('fail', function(test, err) {
48-
failures += 1;
49-
50-
if (err) {
51-
tracebacks.push(stack(err));
52-
}
53-
});
54-
55-
runner.on('end', function() {
56-
// delay posting results a little to capture "multiple-done" errors
57-
setTimeout(function () {
58-
results = {};
59-
results.runtime = (new Date).getTime() - start;
60-
results.total = passes + failures;
61-
results.passed = passes;
62-
results.failed = failures;
63-
results.tracebacks = tracebacks;
64-
results.url = window.location.pathname;
65-
BrowserStack.post("/_report", results, function(){});
66-
}, 1000);
67-
});
68-
};
19+
runner.on('runEnd', function(globalSuite) {
20+
var results = {};
6921

70-
Mocha.BrowserStack.prototype = origReporter.prototype;
22+
// TODO investigate why globalSuite.runtime is not working
23+
results.runtime = 0;
24+
results.total = total;
25+
results.passed = passed;
26+
results.failed = failed;
27+
results.tracebacks = tracebacks;
28+
results.url = window.location.pathname;
7129

72-
return Mocha.BrowserStack;
30+
BrowserStack.post("/_report", results, function() {});
31+
});
7332
})();

lib/server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ exports.Server = function Server(bsClient, workers) {
7171
framework_scripts['mocha'].forEach(function(script) {
7272
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
7373
});
74-
patch += '<script type="text/javascript">mocha.reporter(Mocha.BrowserStack);</script>\n';
7574
} else if (framework === 'qunit') {
7675
framework_scripts['qunit'].forEach(function(script) {
7776
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';

0 commit comments

Comments
 (0)