This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ci-support.js
181 lines (162 loc) · 4.62 KB
/
ci-support.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
(function() {
var files;
var browserId;
var socketEndpoint = window.location.protocol + '//' + window.location.host;
var thisFile = 'ci-support.js';
var thisScript = document.querySelector('script[src$="' + thisFile + '"]');
var base = thisScript.src.substring(0, thisScript.src.lastIndexOf('/')+1);
var tools = {
'mocha-tdd': [
base + 'mocha/mocha.css',
base + 'mocha/mocha.js',
base + 'mocha-htmltest.js',
function() {
var div = document.createElement('div');
div.id = 'mocha';
document.body.appendChild(div);
mocha.setup({ui: 'tdd', slow: 1000, timeout: 10000, htmlbase: ''});
}
],
'chai': [
base + 'chai/chai.js'
]
};
function addFile() {
var file = files.shift();
if (Object.prototype.toString.call(file) == '[object Function]') {
file();
nextFile();
}
else if (file.slice(-3) == '.js') {
var script = document.createElement('script');
script.src = file;
script.onload = nextFile;
script.onerror = function() { console.error('Could not load ' + script.src); };
document.head.appendChild(script);
} else if (file.slice(-4) == '.css') {
var sheet = document.createElement('link');
sheet.rel = 'stylesheet';
sheet.href = file;
document.head.appendChild(sheet);
nextFile();
}
}
function nextFile() {
if (files.length) {
addFile();
} else {
startMocha();
}
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return(false);
}
function runTests(setup) {
browserId = getQueryVariable('browser');
files = [];
if (browserId) {
files.push(socketEndpoint + '/socket.io/socket.io.js');
}
if (typeof setup == 'string') {
var xhr = new XMLHttpRequest();
xhr.open('GET', setup);
xhr.responseType = 'application/json';
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
setupTests(JSON.parse(xhr.response));
}
};
} else {
setupTests(setup);
}
}
function setupTests(setup) {
if (setup.tools) {
setup.tools.forEach(function(tool) {
if (tools[tool]) {
files = files.concat(tools[tool]);
} else {
console.error('Unknown tool: ' + tool);
}
});
}
if (setup.dependencies) {
files = files.concat(setup.dependencies.map(function(d) {
return '../' + d;
}));
}
files = files.concat(setup.tests);
nextFile();
}
function startMocha() {
var runner = mocha.run();
var socket;
if (browserId) {
socket = io(socketEndpoint);
}
var emitEvent = function(event, data) {
var payload = {browserId: browserId, event: event, data: data};
console.log('client-event:', payload);
if (!socket) return;
socket.emit('client-event', payload);
};
var getTitles = function(runnable) {
var titles = [];
while (runnable && runnable.title) {
titles.unshift(runnable.title);
runnable = runnable.parent;
}
return titles;
};
var getState = function(runnable) {
if (runnable.state === 'passed') {
return 'passing';
} else if (runnable.state == 'failed') {
return 'failing';
} else if (runnable.pending) {
return 'pending';
} else {
return 'unknown';
}
};
var cleanError = function(error) {
if (!error) return undefined;
return {message: error.message, stack: error.stack};
};
// the runner's start event has already fired.
emitEvent('browser-start', {
total: runner.total,
url: window.location.toString(),
});
// We only emit a subset of events that we care about, and follow a more
// general event format that is hopefully applicable to test runners beyond
// mocha.
//
// For all possible mocha events, see:
// https://github.com/visionmedia/mocha/blob/master/lib/runner.js#L36
runner.on('test', function(test) {
emitEvent('test-start', {test: getTitles(test)});
});
runner.on('test end', function(test) {
emitEvent('test-end', {
state: getState(test),
test: getTitles(test),
duration: test.duration,
error: cleanError(test.err),
});
});
runner.on('end', function() {
emitEvent('browser-end');
});
}
window.runTests = runTests;
})();