Skip to content

Instantly share code, notes, and snippets.

@victornswd
Last active December 18, 2015 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victornswd/5781326 to your computer and use it in GitHub Desktop.
Save victornswd/5781326 to your computer and use it in GitHub Desktop.
Custom reporter for Intern (https://github.com/theintern/intern) that sends test results (failed/passed, name, duration etc.) to Sauce Labs (https://saucelabs.com/docs/javascript-unit-tests-integration). For usage, check this out: https://github.com/theintern/intern/wiki/Running-Tests
define(
[
'intern/node_modules/dojo/node!intern/node_modules/wd/node_modules/request'
],
function (request) {
// NOTE: For proper testing multiple unit tests in multiple environments,
// set `maxConcurrency` to **1**.
// NOTE: For now you can't start testing with a nested test.
// NOTE: Functional suites are also added to the object sent to Sauce
// Labs.
// TODO: Fix duration calculation, because it's busted now
// Final components
var completeObj = {};
var sessionRuntime = 0;
// Spec components
var globalTestRuntime = 0;
var testRuntime = 0;
var globalPass = true;
// Sauce Labs auth
var username = process.env.SAUCE_USERNAME;
var accessKey = process.env.SAUCE_ACCESS_KEY;
var objExtend = function(obj, props) {
var i;
for (i in props) {
obj[i] = props[i];
}
return obj;
};
var setSuiteName = function(test){
var tempArr = test.id.split(' - ');
tempArr = tempArr.splice( 1, tempArr.indexOf(test.name) - 1 );
return tempArr;
};
var globalPassFn = function(test){
if (!test.hasPassed) {
globalPass = false;
}
};
var api = function (url, method, data) {
request({
method: method,
uri: [
'https://',
username,
':',
accessKey,
'@saucelabs.com/rest',
url
].join(''),
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
});
};
var mockObj = function(obj, name){
objExtend(obj, {
'durationSec': globalTestRuntime,
'passed': globalPass,
'specs': [],
'description': name
});
};
var pushObj = function(main, extra){
main.push(extra);
};
var extendMain = function(mainObj, newObj){
mainObj.passed = newObj.passed;
mainObj.specs = mainObj.specs.concat(newObj.specs);
};
var addSuite = function(suiteName, completeObj, obj) {
// TODO: Fix if nested test is first
// TODO: Add deeper nesting support
// Separate nested from single
if ( suiteName.length === 2 ) {
var suites, _i, _len, _ref;
_ref = completeObj.suites;
// Check for nested's parents
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
suites = _ref[_i];
// Extend nested's parent
if (suites.description === suiteName[0]) {
finalObj(obj, suiteName[suiteName.length - 1], suites);
}
}
} else {
finalObj(obj, suiteName[0], completeObj);
}
};
// Suite
var suiteObj = function(obj, name){
// Mock object
var tempObj = {};
mockObj(tempObj, name);
tempObj.specs.push(obj);
return tempObj;
};
// Complete
var finalObj = function(obj, name, finalObj){
var complete = suiteObj(obj, name);
var x, _i, _len;
var startNewDescription = 0;
// Create suites array on the object
if (!finalObj.suites) {
finalObj.suites = [];
}
// Check if suite needs to be extended or added
_len = finalObj.suites.length;
for (_i = 0; _i < _len; _i++) {
x = finalObj.suites[_i];
if (x.description === name) {
extendMain(x, complete);
} else {
startNewDescription += 1;
}
}
// Add new suite to object
if (startNewDescription === _len) {
pushObj(finalObj.suites, complete);
}
};
var obj = {
'/test/start': function (test) {
// Runtime
testRuntime = Date.now();
},
'/test/end': function (test) {
// Runtime
testRuntime = Date.now() - testRuntime;
testRuntime = testRuntime/1000;
// Test time & success
globalTestRuntime = globalTestRuntime + testRuntime;
globalPassFn(test);
// Set test suite name
var suiteName = setSuiteName(test);
// Create temp obj
var obj = {
'description': test.name,
'durationSec': testRuntime,
'passed': test.hasPassed
};
addSuite(suiteName, completeObj, obj);
},
'/session/start': function(){
// Runtime
sessionRuntime = Date.now();
},
'/session/end': function(wd){
// Runtime
sessionRuntime = Date.now() - sessionRuntime;
// Add completed suite to the final object
objExtend(completeObj, {
'durationSec': sessionRuntime/1000,
'passed': globalPass
});
// Create data element to pass to Sauce Labs & clear everything
var data = {
'custom-data': { intern: completeObj },
'passed': globalPass
};
completeObj = {};
globalPass = true;
globalTestRuntime = 0;
// Send to Sauce Labs
return api(
['/v1/', username, '/jobs/', wd._wd.sessionID].join(''),
'PUT',
data
);
}
};
return obj;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment