Created
November 22, 2012 12:20
-
-
Save keithamus/4130885 to your computer and use it in GitHub Desktop.
How to do Dependency Injection in RequireJS?
This file contains hidden or 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
define([], function () { | |
return function HypotheticalHelperMethod() { | |
doSomeOtherStuff(); | |
} | |
}); | |
define(["helperMethod"], function (helperMethod) { | |
MyClass = function () { | |
this.init() | |
} | |
MyClass.prototype.init = function () { | |
helperMethod(1, 2, 3); | |
} | |
}); |
This file contains hidden or 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(["helperMethod", "myClass"], function (helperMethod, myClass) { | |
describe("MyClass tests", function () { | |
it("calls `helperMethod` upon init", function () { | |
// here i need to spyOn helperMethod, but if I set helperMethod | |
// to a jasmime spy: | |
helperMethod = jasmine.createSpy("helperMethod"); | |
// I've altered my reference, and MyClass still has the original | |
// HypotheticalHelperMethod function. | |
var instance = new MyClass(); | |
expect(helperMethod).toHaveBeenCalledWith(1, 2, 3); | |
}); | |
}); | |
}); |
Ah, OK. If you want to just cram in a modification after a module has been created but before listeners are given a reference to it, you can use the semi-private onResourceLoad API:
https://github.com/jrburke/requirejs/wiki/Internal-API:-onResourceLoad
Note this API is always subject to change. It has been stable for a while, but no guarantees for the future, even though I have no immediate plans to change it.
To modify the value passed to modules dependent on the current module, this would work:
requirejs.onResourceLoad = function (context, map, depArray) {
var id = map.id,
internalModule = context.registry[id],
existingExport = context.defined[id];
//modify existing export here
//If the modification is a modification to the base export (like
//if the export was a function) and not just a property
//modification on the export, hard set the new module value:
internalModule.exports = context.defined[id] = newExportValue;
};
This onResourceLoad definition should be done after require.js loads, but before any module loading is done, at least for modules that you want to intercept.
@keithamus did you find a way to test that your helperMethod is called?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe I haven't explained my self best, so here goes:
I think the real problem I have is because of the declarative nature of require, and the fact that a module is loaded when its dependencies are met, rather than when it itself is called as a dependency. Because of these issues it becomes really difficult to get code before dependency resolution, which is the typical pattern I'd use to spy on something.
If AMD was not involved I would have to either expose the module via name spacing (which makes it easy to spy on) or if I was using some Node Style module loader which requires dependencies imperatively I could stub over the require function to reroute it (aka behaviour like Syringe or Rewire). Both of these means I can mock or spy a module imperatively -- and inline to the tests -- and restore old functionality when I'm done.
Meanwhile AMDs solution seems to be to provide an additional layer of complexity - in that if I want to spy on a module I need to alter the config and set up spies independent of tests. I was hoping for something which could be managed purely inside the test environment, rather than having additional config files to manage tests.