Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created February 9, 2013 03:43
Show Gist options
  • Select an option

  • Save liammclennan/4743734 to your computer and use it in GitHub Desktop.

Select an option

Save liammclennan/4743734 to your computer and use it in GitHub Desktop.
Node.js dependency injection
var declaration = require('./declaration');
declaration();
// --> assertion error 3 is not equal to 4
declaration({
assert: {
equal: function () {
return true;
}
}
});
// -->
function resolve(deps, name) {
if (deps && deps[name]) {
return deps[name];
} else {
return require(name);
}
}
module.exports = function(deps) {
var assert = resolve(deps, 'assert');
assert.equal(3,4);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment