Mutate getters (that return functions) into mockable/spyable functions.

npm install cocoon
2 downloads in the last week
12 downloads in the last month


A small utility module for wrapping property getters within functions.

It is useful as a TDD helper for creating mockable/spyable methods on top of another library's methods that are returned via a getter.

Note: This is not a cure-all. Objects could be frozen, etc.


    var lib = {}
    function foo() {}
    lib.__defineGetter__('foo', function () { return foo })

With something like sinon, you can't (at the moment) mock setters.

With cocoon, you can:

    var cocoon = require('cocoon')

    var c = cocoon


    sinon.stub(lib, 'foo') // works



However.. if:

    var self = {}

    function foo() {}
    self.__defineGetter__('foo', function () { return foo })
    module.exports = self

..Nope. Not going to happen.

npm loves you