Skip to content

Commit

Permalink
Update web runner to work with new error API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Thomas committed Oct 21, 2010
1 parent e509041 commit 89a72ab
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 93 deletions.
31 changes: 25 additions & 6 deletions lib/web-runner.js
Expand Up @@ -181,6 +181,9 @@ exports.run = function(list, options) {
status.started = suites[cmd[0]];
status.started.parallel = cmd[1];

// scope
var started = status.started;

var opts =
{ name: status.started.name
, testName: options.testName
Expand All @@ -206,7 +209,7 @@ exports.run = function(list, options) {
worker = null;
if (code !== 0) {
// worker exited with an error
workerHandlers.suiteError({cmd: 'suiteError'});
workerHandlers.suiteError({cmd: 'suiteError', suite: started});
}
}
}
Expand Down Expand Up @@ -267,8 +270,12 @@ exports.run = function(list, options) {

var socketHandlers =
{ enqueueSuite: function(obj, client) {
//TODO reload the suite from file (if applicable) here

for (var i = 0; i < queue.length; i++) {
if (queue[i][0] == obj.index) {
// don't add a suite to the queue, that is already in it
return;
}
}
queue.push([obj.index, obj.parallel]);
var msg = {cmd: 'queued', index: obj.index, parallel: obj.parallel};
client.broadcast(JSON.stringify(msg));
Expand All @@ -281,7 +288,9 @@ exports.run = function(list, options) {
return;
}

worker.terminate();
if (worker) {
worker.terminate();
}
socket.broadcast(JSON.stringify({cmd: 'cancelled'}));
cleanupSuite();
}
Expand All @@ -298,8 +307,10 @@ exports.run = function(list, options) {
cleanupSuite();
}
, suiteError: function(obj) {
socket.broadcast(JSON.stringify(obj));
cleanupSuite();
if (status.started && status.started == obj.suite) {
socket.broadcast(JSON.stringify(obj));
cleanupSuite();
}
}
, testStart: function(obj) {
var name = obj.name;
Expand All @@ -316,6 +327,14 @@ exports.run = function(list, options) {
var msg = {cmd: 'testDone', result: result};
socket.broadcast(JSON.stringify(msg));
}
, error: function(obj) {
socket.broadcast(JSON.stringify(obj));
if (worker) {
worker.terminate();
worker = null;
}
cleanupSuite();
}
}
}

Expand Down
135 changes: 74 additions & 61 deletions lib/web-runner/public/main.js
Expand Up @@ -87,71 +87,31 @@ io.setPath('/client/');
test.el.className += ' running';
}
, testDone: function(obj) {
if (obj.result.failure && obj.result.failure.constructor == Array) {
curSuite.numErrors++;
if (!curSuite.multiErrors) {
curSuite.multiErrors = [];
}

for (var i = 0; i < obj.result.failure.length; i++) {
for (var j = 0; j < curSuite.multiErrors.length; j++) {
if (obj.result.failure[i].index == curSuite.multiErrors[j].index) {
break;
}
}

if (j == curSuite.multiErrors.length) {
curSuite.multiErrors.push(obj.result.failure[i]);
}
}
// normal error
var test = curSuite.tests[obj.result.name];
var el = test.el;
var status = obj.result.failure ? 'failure' : 'success';
removeClass(el, 'running');
el.className += ' ' + status;

var test = curSuite.tests[obj.result.name];
var el = test.el;
removeClass(el, 'running');
el.className += ' error';
if (obj.result.failure) {
curSuite.numFailures++;

var code = document.createElement('P');
var code = document.createElement('CODE');
code.className = 'result';
code.innerHTML = 'The specific error for this test could not be determined. See the \'Non-specific Errors\' below.';
code.innerHTML = (obj.result.failure).stack;
el.insertBefore(code, el.lastChild);

var summarySpan = document.createElement('SPAN');
summarySpan.className = 'summary';
summarySpan.innerHTML = 'Non-specific error';
summarySpan.innerHTML = (obj.result.failure).message;
test.nameEl.appendChild(summarySpan);

}
else {
// normal error
var test = curSuite.tests[obj.result.name];
var el = test.el;
removeClass(el, 'running');
el.className += ' ' + obj.result.status;

if (obj.result.failure) {
curSuite[obj.result.status == 'error' ? 'numErrors' : 'numFailures']++;

var code = document.createElement('CODE');
code.className = 'result';
code.innerHTML = (obj.result.failure).stack;
el.insertBefore(code, el.lastChild);

var summarySpan = document.createElement('SPAN');
summarySpan.className = 'summary';
summarySpan.innerHTML = (obj.result.failure).message;
test.nameEl.appendChild(summarySpan);
}
}
}
, suiteDone: function(obj) {
var el = curSuite.el;

var multiErrors = [];

if (curSuite.numErrors > 0) {
var status = 'error';
}
else if (curSuite.numFailures > 0) {
if (curSuite.numFailures > 0) {
var status = 'failure';
}
else {
Expand All @@ -160,31 +120,85 @@ io.setPath('/client/');

updateFavicon(status);

if (curSuite.multiErrors) {
removeClass(el, 'running');
el.className += ' '+status;

var input = el.getElementsByTagName('input')[0];
input.disabled = false;

var doneSpan = document.createElement('SPAN');
doneSpan.className = 'done';
doneSpan.innerHTML = 'Done';
el.insertBefore(doneSpan, curSuite.runSpan);

var suite = curSuite;
doneSpan.onclick = function(e) {
clearSuiteResults(suite);
}

curSuite.runSpan.innerHTML = 'Run';

curSuite = null;
}
, error: function(obj) {
var el = curSuite.el;

if (obj.tests.length > 1) {
for (var i = 0; i < obj.tests.length; i++) {
var test = curSuite.tests[obj.tests[i]];
removeClass(test.el, 'running');
test.el.className += ' error';

var code = document.createElement('P');
code.className = 'result';
code.innerHTML = 'The specific error for this test could not be determined. See the \'Non-specific Error\' below.';
test.el.insertBefore(code, test.el.lastChild);

var summarySpan = document.createElement('SPAN');
summarySpan.className = 'summary';
summarySpan.innerHTML = 'Non-specific error';
test.nameEl.appendChild(summarySpan);
}

var li = document.createElement('LI');
li.className = 'non-specific-errors';

var span = document.createElement('SPAN');
span.className = 'name';
span.innerHTML = 'Non-specific Errors';
span.innerHTML = 'Non-specific Error';

span.onclick = function(e) {
toggleItem({el: li});
}
li.appendChild(span);

var errors = curSuite.multiErrors;
code = document.createElement('CODE');
code.className = 'result';
for (var i = 0; i < errors.length; i++) {
code.appendChild(document.createTextNode(errors[i].stack));
code.appendChild(document.createElement('BR'));
}
code.appendChild(document.createTextNode(obj.error.stack));
li.appendChild(code);

curSuite.el.getElementsByClassName('tests-list')[0].appendChild(li);
el.getElementsByClassName('tests-list')[0].appendChild(li);
}
else {
var test = curSuite.tests[obj.tests[0]];

removeClass(test.el, 'running');
test.el.className += ' error';

var code = document.createElement('CODE');
code.className = 'result';
code.innerHTML = obj.error.stack;
test.el.insertBefore(code, test.el.lastChild);

var summarySpan = document.createElement('SPAN');
summarySpan.className = 'summary';
summarySpan.innerHTML = obj.error.message;
test.nameEl.appendChild(summarySpan);
}

var status = 'error';
updateFavicon(status);

removeClass(el, 'running');
el.className += ' '+status;

Expand Down Expand Up @@ -237,7 +251,6 @@ io.setPath('/client/');

suite.numFailures = 0;
suite.numErrors = 0;
delete suite.multiErrors;

var els = suite.el.getElementsByClassName('non-specific-errors');
while(els.length) {
Expand Down
41 changes: 15 additions & 26 deletions lib/web-runner/worker-test-runner.js
Expand Up @@ -20,9 +20,10 @@ onmessage = function(message) {
, testName: details.testName
, onSuiteStart: suiteStart
, onSuiteDone: suiteDone
, onPrematureExit: prematureExit
, onTestStart: testStart
, onTestDone: testDone
, onError: error
, onPrematureExit: prematureExit
};

try {
Expand Down Expand Up @@ -68,35 +69,23 @@ function testDone(result) {
if (closed) { return; }

if (result.failure) {
if (result.failure.length) {
result.status = 'error';
result.failure = result.failure.map(function(el) {
var index = multiErrors.indexOf(el);
if (index < 0) {
index = multiErrors.length;
multiErrors.push(el);
}
return {
message: el.message
, index: index
, stack: el.stack.replace(stackReplaceRegExp, '.')
};
});
}
else {
result.status = result.failure instanceof assert.AssertionError ? 'failure' : 'error';
result.failure =
{ message: result.failure.message
, stack: result.failure.stack.replace(stackReplaceRegExp, '.')
};
}
}
else {
result.status = 'success';
result.failure =
{ message: result.failure.message
, stack: result.failure.stack.replace(stackReplaceRegExp, '.')
}
}

postMessage({cmd: 'testDone', result: result});
}
function error(err, tests) {
if (closed) { return; }

var e = { message: err.message
, stack: err.stack.replace(stackReplaceRegExp, '.')
};

postMessage({cmd: 'error', error: e, tests: tests});
}
function prematureExit(tests) {
if (closed) { return; }

Expand Down
7 changes: 7 additions & 0 deletions test/error-async.js
@@ -1,5 +1,12 @@

module.exports = {
'test passes': function(test) {
setTimeout(function() {
test.ok(true);
test.finish();
}, 500);
},

'test async error 1': function(test) {
setTimeout(function() {
throw new Error('error 1');
Expand Down

0 comments on commit 89a72ab

Please sign in to comment.