flacon
flacon is a dependency injection container with a clean and tiny API. It helps you to test your components individually.

Installing
npm install flacon
Introduction
Imagine you have a service foo.js
.
module.exports = {
value: function () { return 'foo' }
};
Now you want another service bar.js
that uses foo.js
.
var foo = require('./foo');
module.exports = {
value: function () { return foo.value() + 'bar' }
};
This looks all good. But when testing bar.js
, mocking foo.js
is really difficult because it is a private dependency. flacon, on the other hand, forces you to explicitly declare all dependencies, making it easy to mock them.
publish
First, we create a new flacon container in container.js
.
var Flacon = require('flacon');
module.exports = new Flacon();
You can do two things with a container: publish
a module and load
a module.
Let's publish foo
first. We call the publish
method with an id and a factory function.
var container = require('./container');
container.publish('foo', function () {
return {
value: function () { return 'foo' }
};
});
Moving on to bar
, we define foo
as a dependency. All dependencies get passed into the factory function.
var container = require('./container');
container.publish('bar', ['foo'], function (foo) {
return {
value: function () { return foo.value() + ' bar' }
};
});
load
By simply calling the container with a module id, you will get the return value of the factory function.
var container = require('./container');
var bar = container('bar');
bar.value();
During testing, we can easily manipulate or mock a dependency.
var container = require('./container');
var bar = container('bar', {
'a': function (a) {
a.value = function () { return 'baz' }
return a;
}
});
bar.value();
Contributing
If you have a question, found a bug or want to propose a feature, have a look at the issues page.