Skip to content

Instantly share code, notes, and snippets.

@alex-seville
Created October 29, 2012 16:22
Show Gist options
  • Save alex-seville/3974605 to your computer and use it in GitHub Desktop.
Save alex-seville/3974605 to your computer and use it in GitHub Desktop.
custom loading in node
var fs = require("fs");
var oldLoader = require.extensions['.js'];
//find current scripts
require.extensions['.js'] = function(module, filename) {
if (filename.indexOf("/src/") > -1){
var content = fs.readFileSync(filename, 'utf8');
// Parse the file content and give to module.exports
var covintro = "/* automatically generated by JSCoverage - do not edit */";
covintro += " if (typeof _$jscoverage === 'undefined'){ _$jscoverage = {};}";
covintro += " if (! _$jscoverage['srcScript.js']) {";
covintro += " _$jscoverage['srcScript.js'] = [];";
covintro += " _$jscoverage['srcScript.js'][1] = 0;";
covintro += " _$jscoverage['srcScript.js'][2] = 0;";
covintro += " _$jscoverage['srcScript.js'][1]++;";
covintro += "} ";
//not the actual source, just for simplicity sake
covintro += " _$jscoverage['srcScript.js'].source = [\"source line1\",\"source line2\"];";
covintro += " global._$jscoverage = _$jscoverage;";
content = covintro + content;
module.exports = eval(content);
}else{
oldLoader(module,filename);
}
};
module.exports = function sample(){
return true;
};
var blanket = require("../lib/blanket"),
assert = require("assert"),
scrFile = require("../src/srcScript");
describe('easy test', function(){
describe('simplest', function(){
it('should return true when called', function(){
assert.equal(scrFile(), true);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment