Created
August 9, 2011 21:00
-
-
Save jbpros/1135181 to your computer and use it in GitHub Desktop.
Stub node modules with jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would you know how to extend browserify's require to tie this in? It doesn't seem to use
require.extensions
.