Add support for Sauce reports to qunit-extras.

This commit is contained in:
John-David Dalton
2014-09-04 23:57:07 -07:00
parent 9ce6d4b72a
commit 379da81f85

View File

@@ -178,10 +178,6 @@
*/ */
function runInContext(context) { function runInContext(context) {
/** Used to report the test module for failing tests */
var moduleName,
modulePrinted;
/** Object references */ /** Object references */
var phantom = context.phantom, var phantom = context.phantom,
define = context.define, define = context.define,
@@ -403,21 +399,39 @@
*/ */
QUnit.config.extrasData = { QUnit.config.extrasData = {
'logs': [],
/** /**
* An array of details for each log entry. * An array of details for each log entry.
* *
* @memberOf QUnit.config.extrasData * @memberOf QUnit.config.extrasData
* @type Array * @type Array
*/ */
'logEntries': [] 'module': {}
}; };
// add a callback to be triggered when all testing has completed // add a callback to be triggered when all testing has completed
QUnit.done(function(details) { QUnit.done(function(details) {
// assign results to `global_test_results` for Sauce Labs // assign results to `global_test_results` for Sauce Labs
details.tests = QUnit.config.extrasData.logs;
context.global_test_results = details; context.global_test_results = details;
}); });
// add a callback to be triggered after every assertion
QUnit.log(function(details) {
var data = QUnit.config.extrasData;
data.logs.push(details);
data.module.logs.push(details);
});
// add a callback to be triggered at the start of every test module
QUnit.moduleStart(function(details) {
var module = QUnit.config.extrasData.module;
module.name = details.name;
module.logs = [];
module.printed = false;
});
// add a callback to be triggered at the start of every test // add a callback to be triggered at the start of every test
QUnit.testStart(function(details) { QUnit.testStart(function(details) {
var config = QUnit.config, var config = QUnit.config,
@@ -435,14 +449,14 @@
config = QUnit.config, config = QUnit.config,
index = -1, index = -1,
length = asserts.length, length = asserts.length,
entries = config.extrasData.logEntries, logs = config.extrasData.module.logs,
queue = config.queue; queue = config.queue;
while (++index < length) { while (++index < length) {
var assert = asserts[index]; var assert = asserts[index];
if (!assert.result && this.retries < config.asyncRetries) { if (!assert.result && this.retries < config.asyncRetries) {
if (!isSilent) { if (!isSilent) {
entries.length -= asserts.length; logs.length -= asserts.length;
} }
this.retries++; this.retries++;
asserts.length = 0; asserts.length = 0;
@@ -528,104 +542,78 @@
// add logging extras // add logging extras
if (!isSilent) { if (!isSilent) {
// add a callback to be triggered when all testing has completed // add a callback to be triggered when all testing has completed
QUnit.done(function() { QUnit.done(function(details) {
var ran; var failures = details.failed,
return function(details) { statusColor = failures ? 'magenta' : 'green';
// stop `asyncTest()` from erroneously calling `done()` twice in
// environments w/o timeouts logInline();
if (ran) { console.log(hr);
return; console.log(color(statusColor, ' PASS: ' + details.passed + ' FAIL: ' + failures + ' TOTAL: ' + details.total));
console.log(color(statusColor, ' Finished in ' + details.runtime + ' milliseconds.'));
console.log(hr);
// exit out of Node.js or PhantomJS
try {
if (failures) {
process.exit(1);
} else {
process.exit(0);
} }
ran = true; } catch(e) {}
var failures = details.failed; // exit out of Narwhal, Rhino, or RingoJS
var statusColor = failures ? 'magenta' : 'green'; try {
logInline(); if (failures) {
console.log(hr); java.lang.System.exit(1);
console.log(color(statusColor, ' PASS: ' + details.passed + ' FAIL: ' + failures + ' TOTAL: ' + details.total)); } else {
console.log(color(statusColor, ' Finished in ' + details.runtime + ' milliseconds.')); quit();
console.log(hr); }
} catch(e) {}
// exit out of Node.js or PhantomJS
try {
if (failures) {
process.exit(1);
} else {
process.exit(0);
}
} catch(e) {}
// exit out of Narwhal, Rhino, or RingoJS
try {
if (failures) {
java.lang.System.exit(1);
} else {
quit();
}
} catch(e) {}
};
}());
// add a callback to be triggered after every assertion
QUnit.log(function(details) {
QUnit.config.extrasData.logEntries.push(details);
});
// add a callback to be triggered at the start of every test module
QUnit.moduleStart(function(details) {
// reset the `modulePrinted` flag
var newModuleName = details.name;
if (moduleName != newModuleName) {
moduleName = newModuleName;
modulePrinted = false;
}
}); });
// add a callback to be triggered after a test is completed // add a callback to be triggered after a test is completed
QUnit.testDone(function(details) { QUnit.testDone(function(details) {
var config = QUnit.config, var config = QUnit.config,
module = config.extrasData.module,
logs = module.logs,
failures = details.failed, failures = details.failed,
hidepassed = config.hidepassed, hidepassed = config.hidepassed;
entries = config.extrasData.logEntries.slice(),
testName = details.name;
config.extrasData.logEntries.length = 0;
if (hidepassed && !failures) { if (hidepassed && !failures) {
return; return;
} }
logInline(); logInline();
if (!modulePrinted) { if (!module.printed) {
modulePrinted = true; module.printed = true;
console.log(hr); console.log(hr);
console.log(color('bold', moduleName)); console.log(color('bold', module.name));
console.log(hr); console.log(hr);
} }
console.log(' ' + (failures ? color('red', 'FAIL') : color('green', 'PASS')) + ' - ' + testName); console.log(' ' + (failures ? color('red', 'FAIL') : color('green', 'PASS')) + ' - ' + details.name);
if (!failures) { if (!failures) {
return; return;
} }
var index = -1, var index = -1,
length = entries.length; length = logs.length;
while(++index < length) { while(++index < length) {
var entry = entries[index]; var log = logs[index];
if (hidepassed && entry.result) { if (hidepassed && log.result) {
continue; continue;
} }
var expected = entry.expected, var expected = log.expected,
result = entry.result, result = log.result,
type = typeof expected != 'undefined' ? 'EQ' : 'OK'; type = typeof expected != 'undefined' ? 'EQ' : 'OK';
var message = [ var message = [
result ? color('green', 'PASS') : color('red', 'FAIL'), result ? color('green', 'PASS') : color('red', 'FAIL'),
type, type,
entry.message || 'ok' log.message || 'ok'
]; ];
if (!result && type == 'EQ') { if (!result && type == 'EQ') {
message.push(color('magenta', 'Expected: ' + expected + ', Actual: ' + entry.actual)); message.push(color('magenta', 'Expected: ' + expected + ', Actual: ' + log.actual));
} }
console.log(' ' + message.join(' | ')); console.log(' ' + message.join(' | '));
} }