forked from pbakaus/mocha-sauce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
135 lines (100 loc) · 2.78 KB
/
client.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
function FancyJSON(runner) {
var root = null;
var result = {};
function recurse(suite, result) {
result.durationSec = 0;
result.passed = true;
if(suite.title)
result.description = suite.title;
if(suite.tests.length) {
result.specs = [];
for (var i = 0; i < suite.tests.length; i++) {
result.specs.push({
"description": suite.tests[i].title,
"durationSec": (suite.tests[i].duration / 1000) || 0, // duration of spec run in seconds
"passed": suite.tests[i].state === "passed" // did the spec pass?
//"passedCount": 1, // passed assertions in spec
//"failedCount": 0, // failed assertions in spec
//"totalCount": 1 // total assertions in spec
});
result.durationSec += (suite.tests[i].duration / 1000) || 0;
if(suite.tests[i].state !== "passed")
result.passed = false;
}
}
if(suite.suites.length) {
result.suites = [];
var sub = null;
for (var j = 0; j < suite.suites.length; j++) {
sub = {};
recurse(suite.suites[j], sub);
result.suites.push(sub);
result.durationSec += sub.durationSec || 0;
if(!sub.passed)
result.passed = false;
}
}
}
runner.on('suite', function(suite) {
if(suite.parent.root) root = suite.parent;
});
runner.on('end', function() {
recurse(root, result);
window.jsonReport = result;
});
}
function mochaSaucePlease(options, fn) {
(function(runner) {
// execute optional callback to give user access to the runner
if(fn) {
fn(runner);
}
if(!options) {
options = {};
}
// in a PhantomJS environment, things are different
if(!runner.on) {
return;
}
// Generate JSON coverage
mocha.reporter(FancyJSON);
new mocha._reporter(runner);
// Generate XUnit coverage
window.xUnitReport = 'off';
if(options.xunit != false) {
window.xUnitReport = '';
(function() {
var log = window.console && console.log;
if(!window.console) {
window.console = {};
}
console.log = function() {
window.xUnitReport += arguments[0] + "\n"; // TODO: handle complex console.log
if(log) log.apply(console, arguments);
};
})();
mocha.reporter("xunit", {});
new mocha._reporter(runner);
}
// The Grid view needs more info about failures
var failed = [];
runner.on('fail', function(test, err) {
failed.push({
title: test.title,
fullTitle: test.fullTitle(),
error: {
message: err.message,
stack: err.stack
}
});
});
// implement custom reporter for console to read back from Sauce
runner.on('end', function() {
runner.stats.failed = failed;
runner.stats.xUnitReport = xUnitReport;
runner.stats.jsonReport = jsonReport;
window.mochaResults = runner.stats;
window.chocoReady = true;
});
})(window.mochaPhantomJS ? mochaPhantomJS.run() : mocha.run());
}