New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cocoon

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cocoon

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

  • 0.1.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Cocoon

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.

Example

    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
                .spin(lib)
                .wrap('foo')

    ..

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

    c.restore()

Yay.

However.. if:

    var self = {}

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

..Nope. Not going to happen.

FAQs

Package last updated on 01 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc