I'm using JsTestDriver to test code written for Parse's CloudCode, and as the project grows, I'm segmenting the code into modules. Some of the modules have nested dependencies (e.g. a common util module), so they need to load recursively. Some of these modules are loaded by various tests, so I need to figure out how to load them from JsTestDriver.
Parse CloudCode uses a common.js style require(), which I'm attempting to emulate when running in the context of the browser. I've implemented the emulation like so:
function require(filename)
{
var savedExports = window.exports;
window.exports = {};
// open and send a synchronous request
var xhrObj = new XMLHttpRequest();
xhrObj.open('GET', filename, false);
xhrObj.send('');
// add the returned content to a newly created script tag
var se = document.createElement('script');
se.type = "text/javascript";
se.text = xhrObj.responseText;
document.getElementsByTagName('head')[0].appendChild(se);
var returnVal = window.exports;
window.exports = savedExports;
return returnVal;
};
Sadly, the response text is never the requested module .js file, but always a full html document for the test harness itself. My jsTestDriver.conf contains the following entries:
load:
- test/lib/parse-1.2.13.js
- test/lib/require-lite.js
- cloud/utils.js
- cloud/backgroundJob.js
- cloud/models/*.js
test:
- test/*.js
serve:
- cloud/*.js
- cloud/models/*.js
Any thoughts on what I'm doing wrong?