npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

independence

v0.6.4

Published

Dependency injection helper

Downloads

9

Readme

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