0.6.4 • Published 9 years ago

independence v0.6.4

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

Independence

NPM version Dependency Status Build Status

Module dependency injection for easy and fast mocking.

Enables a module to quickly create clones of itself that have their dependencies mocked.

When writing the module

Wrap your module in "require('independence')(require, module, (function(require, module, exports) {" + yourCode + "}))".

// monkey.js

require('independence')(require, module, function(require, module, exports){

  var moment = require('moment');
  var _ = require('lodash');
  var myService = require('../common/lib/services/myService');
  var myDatabase = require('../common/lib/myDatabase');

  exports.fling = function() {
    console.log('fling', moment().format 'YYYY-MM-DD');
  };

  exports.swing = function() {
    console.log('swing', _.find([1, 2, 3, 5, 7], (n) -> n % 2 is 0));
  };
});

With CoffeeScript, you can require() the provided coffee-wrap to automatically wrap all your .coffee modules when transpiling them to JavaScript.

When using the module

This works as normal:

// someModule.js

var monkey = require('./monkey');

monkey.fling(); // Will output `fling 2014-05-23`
monkey.swing(); // Will output `swing 2`

When testing the module

// tests/monkey.js

var monkey = require('./monkey');

function momentMock() {
  return {
    format: function() {
      return "Yaaap!";
    }
  };
}


// Provide only moment and leave all other dependencies undefined:
var pureMonkey = monkey.independence('isolate', {moment: momentMock});

pureMonkey.fling() // Outputs `fling Yaaap!`
pureMonkey.swing() // Fails because `_` is an empty Object


// Override moment, but leave all other dependencies nominal:
var testMonkey = monkey.independence('override', {moment: momentMock});

testMonkey.fling() // Outputs `fling Yaaap!`
testMonkey.swing() // Works as normal


// Override moment in the monkey module and in ALL its wrapped dependencies
// recursively (in this case, myService and myDatabase):
var testMonkey = monkey.independence('rebuild', {moment: momentMock});

But what if two modules have the same name?

// controller.js
var userModel = require('../lib/models/user');
var userController = require('./user');

...
// tests/controller.js
var controller = require('../controllers/controller');

var testController = controller.independence('isolate', {
    'models/user': userModelMock,
    'controllers/user': userControllerMock
  });

...

Other stuff you can do

Chain dependency objects:

var commonDependencies = {
  lodash: require('lodash'),
  myDatabase: {log: function(){}},
  myService: function(){ return 'serviced';}
};

var testMonkey = monkey.independence('isolate', commonDependencies, {
  moment: function(){ return {format: function(){ return 'Yip!';}};}
};

Clone & monkey-patch:

var database = require('database');

var databaseTest = database.independence('override');
databaseTest.getBananas = function(callback) {
  callback(null, ['banana1', 'banana2']);
};

Coffee & Mocha

If you're using CoffeeScript and Mocha you can use bin/coffee-wrap to compiles and wraps .coffee modules with the Independence header:

mocha --compilers coffee:independence/bin/coffee-wrap

0.6.4

9 years ago

0.6.3

9 years ago

0.6.2

10 years ago

0.6.1

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago