Skip to content

Instantly share code, notes, and snippets.

@jbpros
Created August 9, 2011 21:00
Show Gist options
  • Save jbpros/1135181 to your computer and use it in GitHub Desktop.
Save jbpros/1135181 to your computer and use it in GitHub Desktop.
Stub node modules with jasmine
require('./spec_helper.js');
describe("something.act()", function() {
it("calls the 'root' function of my module", function() {
var mod = spyOnModule('my_module');
something.act();
expect(mod).toHaveBeenCalled();
});
});
var moduleSpies = {};
var originalJsLoader = require.extensions['.js'];
spyOnModule = function spyOnModule(module) {
var path = require.resolve(module);
var spy = createSpy("spy on module \"" + module + "\"");
moduleSpies[path] = spy;
delete require.cache[path];
return spy;
};
require.extensions['.js'] = function (obj, path) {
if (moduleSpies[path])
obj.exports = moduleSpies[path];
else
return originalJsLoader(obj, path);
}
afterEach(function() {
for (var path in moduleSpies) {
delete moduleSpies[path];
}
});
@seagoj
Copy link

seagoj commented Feb 10, 2015

Would you know how to extend browserify's require to tie this in? It doesn't seem to use require.extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment