Lightweight IOC container for Node.js
Breadboard is an opinionated inversion of control container for Node.js applications.
requireis less than ideal.
requirecalls. This approach is indeterministic, depending on various seemingly unrelated conditions around how the the module you want to mock was defined.
Breadboard will require all your application's dependencies defined in
package.json, all Node native modules and all of your application's modules and store them in a
dependencies object. The object is exposed to your application's modules by calling the modules as functions, passing the dependencies as an argument. As such, your modules are expected to be wrapped in an extra function returning the desired export value, which Breadboard then calls on application start.
npm install breadboard
Consider this CommonJS module:
//startServer.jsconst d = require'debug''myApp';const createServer = require'./lib/createServer';moduleexports = =>const server = createServer;serverlisten80 =>d'Server listening on port 80';;return server;;
The Breadboard equivalent would be:
//startServer.js// wrap module in factory functionmoduleexports = dependencies =>// return the core functionality of the module as a functionreturn =>// destructure dependencies to get the modules neededconstdebug'/lib/createServer': createServer= dependencies;const server = createServer;const d = debug'myApp';serverlisten80 =>d'Server listening on port 80';;return server;;;
NOTE: The destructuring of dependencies has to happen within your returned module function, like in the above example. The wrapper function receives a reference to the
dependencies object which will not be fully populated by the time the wrapper function is executed, as Breadboard iterates over a flat list of your app's modules. So if module A depends on module B, by the time
dependencies are injected into A, they might not contain a reference to module B yet.
dependencies are fully resolved when the entry point of the whole application is executed, but not sooner.
To start your application:
const breadboard = require'breadboard';breadboardentry: '/index'containerRoot: 'app'initialState:arbitrary: 'state data'blacklist: 'newrelic'thenentryPointReturnValue dependencies =>console.log'Application started' entryPointReturnValue dependencies;;
Module keys in a Breadboard app are static, ie. are always relative to the container's root folder, starting with
/, and always using
/ as path separators, no matter the platform. Consider these example module keys:
Keys for native Node.js modules and 3rd party modules remain the same as if you were using
Breadboard also loads all JSON files. To access them, append
.json to the end of the key, eg.
Returns a promise chained to the return value of your application's entry point, which might be another promise or a concrete value.
Module key for the entry point of the application.
Will be called as the entry point module, with resolved dependencies as the argument.
Path relative to the current working directory, from which all module keys will be resolved
The argument the
entry function will be called with.
List of modules from your
package.json which you wish Breadboard not to load. If you want to defer a
require call to a 3rd party module, put it in the
require manually in your code.
A Breadboard module-key to module mapping to indicate which modules you want to substitute across the whole application with your custom implementation. Useful when testing integration of multiple modules. You could substitute eg. a database connector with a stub to remove a running database as a dependency of your tests.
In tests require your Breadboard modules as if they were CommonJS modules. You can then supply your own stubs and spies as test doubles. Consider the following example:
moduleexports = deps =>return =>const'/widgets/createDough': createDough'/pasta/createPapardelle': createPapardelle'debug': debug= deps;const d = debug'pasta';dcreatePapardellecreateDough;;;
import expect from 'chai';import mainFactory from '../../app/index';import sinon from 'sinon';describe'Main' =>const sandbox = sinonsandboxcreate;const debugSpy = sandboxspy;const mockDough = 'dough';const mockDependencies =debug: sandboxstubreturnsdebugSpy'/widgets/createDough': sandboxstubreturnsmockDough'/pasta/createPapardelle': sandboxspy;let main;beforeEach =>main = mainFactorymockDependencies;;afterEach =>sandboxreset;;it'calls debug' =>main;expectmockDependenciesdebugcalledOncetobetrue;;it'calls createDough' =>main;expectmockDependencies'/widgets/createDough'calledOncetobetrue;;it'calls createPapardelle with createDough return value' =>main;expectmockDependencies'/pasta/createPapardelle'calledWithmockDoughtobetrue;;;
autoMock automatically replaces every dependency of a given Breadboard module with a Sinon.JS stub.
autoMock relies on ES6 Proxies and on a Proxy polyfill. This requires Node to be run with the
--harmony-proxies flag. Supposing you used Mocha as the test runner in your project, you would execute:
node --harmony-proxies ./node_modules/mocha/bin/_mocha. Proxies have been implemented in V8 4.9, which has not yet landed in Node. Until then this caveat will require the
The Breadboard factory function to build the subject to test.
A Breadboard module-key to module mapping to indicate which modules you want to mock manually.
The module returned by
The mock dependencies injected into
Instance of a Sinon.JS sandbox.
moduleexports = deps =>returnconst'/widgets/createDough': createDough'/pasta/createPapardelle': createPapardelle'debug': debug= deps;const d = debug'pasta';dcreatePapardellecreateDough;;;
import expect from 'chai';import mainFactory from '../../app/index';import autoMock from 'breadboard/lib/autoMock';const mockDough = 'dough';const subject: main sandbox deps = autoMockmainFactory;const debugSpy = sandboxspy;depsdebugreturnsdebugSpy;deps'/widgets/createDough'returnsmockDough;describe'Main' =>afterEach =>sandboxreset;;it'calls debug' =>main;expectdepsdebugcalledOncetobetrue;;it'calls createDough' =>main;expectdeps'/widgets/createDough'calledOncetobetrue;;it'calls createPapardelle with createDough return value' =>main;expectdeps'/pasta/createPapardelle'calledWithmockDoughtobetrue;;;