Skip to content

Instantly share code, notes, and snippets.

@alex-seville
Created October 24, 2012 17:38
Show Gist options
  • Save alex-seville/3947549 to your computer and use it in GitHub Desktop.
Save alex-seville/3947549 to your computer and use it in GitHub Desktop.
qunit coverage with require
//used to wrap requirejs in our code
var _ = require("underscore"),
fs = require("fs");
var template = fs.readFileSync(__dirname+"/template",'utf8');
var requirejs = fs.readFileSync(__dirname+"/require.js",'utf8');
var compiled = _.template(template);
var result = compiled({requirejs: requirejs});
fs.writeFileSync(__dirname+"/requireBlanket.js",result,'utf8');
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit Example</title>
<!-- QUNIT, we don't touch -->
<link rel="stylesheet" href="qunit.css">
<script src="qunit.js"></script>
<!-- the SRC file to be tested, the user ONLY NEEDS TO ADD DATA-TEST TO THE SRC SCRIPT REFERENCES -->
<script data-test src="sample.js"></script>
<!-- our built script -->
<script src="requireBlanket.js"></script>
<!-- end -->
<!-- users test file -->
<script data-test src="test.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>
//this test should fail
var sampleTest = function(){
return 5;
};
QUnit.config.autostart = false;
<%= requirejs %>
var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
defineRegExp = /(^|[^\.])define\s*\(/,
requireRegExp = /(^|[^\.])require\s*\(\s*['"][^'"]+['"]\s*\)/,
exportsRegExp = /exports\s*=\s*/,
sourceUrlRegExp = /\/\/@\s+sourceURL=/;
requirejs.load = function (context, moduleName, url) {
var hasLocation = typeof location !== 'undefined' && location.href,
defaultHostName = hasLocation && location.hostname,
defaultPort = hasLocation && (location.port || undefined),
defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, '');
requirejs.cget(url, function (content) {
//Determine if a wrapper is needed. First strip out comments.
//This is not bulletproof, but it is good enough for elminating
//false positives from comments.
var temp = content.replace(commentRegExp, '');
content = content.replace("5","10");
content += "\nCOVERAGE=99;";
eval.call(this,content);
context.completeLoad(moduleName);
}, function (err) {
throw err;
});
};
requirejs.createXhr = function () {
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject !== "undefined") {
for (i = 0; i < 3; i += 1) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}
return xhr;
};
requirejs.cget = function (url, callback, errback, onXhr) {
var xhr = requirejs.createXhr();
xhr.open('GET', url, true);
//Allow overrides specified in config
if (onXhr) {
onXhr(xhr, url);
}
xhr.onreadystatechange = function (evt) {
var status, err;
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
status = xhr.status;
if (status > 399 && status < 600) {
//An http 4xx or 5xx error. Signal an error.
err = new Error(url + ' HTTP status: ' + status);
err.xhr = xhr;
errback(err);
} else {
callback(xhr.responseText);
}
}
};
xhr.send(null);
};
var toArray = Array.prototype.slice;
var scripts = toArray.call(document.scripts);
var scriptNames = scripts.filter(function(elem){
return toArray.call(elem.attributes).some(function(es){
return es.nodeName == "data-test";
})
}).map(function(s){
return s.src.replace(".js","").replace("http://127.0.0.1:3000/","");
});
QUnit.done = function(failures, total) {
alert(COVERAGE);
}
require(scriptNames, function() {
QUnit.start();
});
test( "require test", function() {
ok( sampleTest() == 10, "Passed!" );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment