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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sinon-mocha

v0.0.3

Published

Automatic restore for spies & mocks for mocha

Readme

Sinon Mocha

Sinon Mocha is an extension library for mocha/sinon that will automatically clean up "wrapped" methods.

Tested with:

| Sinon | Mocha | | ----- | ----- | | 1.3 | 0.12 |

As of version 0.0.1 browser support is not tested but is planned in a future version.

Example Usage


var sinon = require('sinon'),
    //You don't need to use expect for this to work
    //This is just to provide a working example
    expect = require('expect.js');
    
//Enhance sinon
//You can also pass a mocha as the second argument.
require('sinon-mocha').enhance(sinon);

describe("MyClass", function(){

  var MyClass = require('my-class'),
      subject;
      
  //before (all) hooks are *NOT* automatically cleaned up for you.
  before(function(){
    sinon.stub(MyClass.prototype, 'ajax', function(){
      //...
    });
  });
  
  //You must `.restore` the method yourself.
  after(function(){
    MyClass.prototype.ajax.restore();
  });

  beforeEach(function(){
    subject = new MyClass();
    
    sinon.spy(subject, 'spiedMethod');
    //Stubs also work
    sinon.stub(subject, 'stubbedMethod');
    //And stubs with functionality
    sinon.stub(subject, 'stubbedMethodWFunc', function(){
      console.log(' I get called ! ' );
    });
  });
  
  describe(".spiedMethod", function(){
  
    it("will call spy", function(){
      subject.spiedMethod();
      expect(subject.spiedMethod.called).to.be(true);
    });
    
  });
  
  // Etc...

});

How it works

In short sinon's wrapMethod function is overwritten to capture its results in the beforeEach of every test. Then in the afterEach those methods are .restore()[d].

To gain access to mocha there is a hack that will search through require.cache and pick out its mocha/index.js and require it to ensure we modify the actual mocha being used. I ran into some issue that lead me down this path. When running require('mocha') I would end up with a different instance then was being used inside the test context.

Mocha.Runner.runSuite is patched to add a beforeEach and afterEach that invokes the sinon magic. See lib/mocha-hooks.js.

WHY?

Jasmine spoiled me.

License

MIT (see LICENSE)