node package manager




monkey is a small JavaScript library, written in CoffeeScript for monkey patching dependent functionality into objects. Dependency Injection (DI) is a concept that allows components, such database access, to be swapped in/out during run time instead of compile time.

This allows an application to change its functionality without changing its logic. This is very useful when testing an application, being able to swap in a database component for a fake on that writes to nowhere.

Since JavaScript allows objects to be changed during runtime, it is easy to simply monkey patch in a new piece of functionality for different use cases.


monkey provides two core functions:

  • monkey.patch
  • monkey.unpatch


Here is an example where monkey being used to inject functionality for a test

class User 
    save: () -> # real functionality
    find: (id) -> # look up the user

user = new User
monkey.patch user, 
    save: () -> true
    find: () -> new User 

... run some tests ... 

# revert user back to normal
monkey.unpatch user

# do some other change to user
monkey.patch user, 
    save: () -> false
    find: () -> null

... etc ..