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

mochito

v0.0.2

Published

A hygenic JsMockito wrapper for modern test frameworks like Mocha.

Downloads

30

Readme

Build Status

Mochito

Mochito is a hygenic JsMockito / JsHamcrest wrapper for modern test frameworks like Mocha. It aims to make JsMockito more competitive when compared with Sinon.JS.

Rationale

Despite its popularity, Sinon.JS has a number of negatives, including:

  1. You can't assert on a spy or stub; you can only ask a question that returns a boolean, which provides no error info if it fails.
  2. but, ... you have to use a spy or stub if you need a function rather than an object.
  3. Mocks have to define all expectations up front (leading to overly tight specifications), rather than allowing assertions to be made post-hoc.
  4. As a consequence, you can't do Given-When-Then style testing with Sinon.JS because what would otherwise be the then clause must appear before the when clause.
  5. Mocked methods are only given the ability to verify expectations after some expectations have been added, often preventing the use of the mock when it's first created.
  6. The mock() function returns a configurator object rather than the mock itself, which is somewhat confusing.
  7. You need to remember to verify your expectations in your tear down methods or tests may pass when they shouldn't.
  8. Its deep equality checking method doesn't consider an array to be the same as an argument array containing the same items.

Unfortunately, JsMockito has some downsides too (which Mochito fixes):

  1. It uses lots of global variables rather than allowing you to require a single item.
  2. It's difficult to integrate into your testing framework — as a consequence of using globals.
  3. It requires you to separately include JsHamcrest, but doesn't work if you include a version that isn't exactly the version it's using.
  4. The error messages don't read very well because JsMockito throws strings which are subsequently converted to Error instances by the containing test framework (will be fixed by this issue).

Usage

Using mochito is simple, for example:

var mochito = require('mochito');

describe('mochito', function() {
  it('allows mocking to be performed with no set-up required', function() {
    var mock = mochito.mock({
      doSomething: function() {}
    });

    mock.doSomething('x', 'y');
    mock.doSomething('z', 'y');

    mochito.verify(mock, mochito.once()).doSomething('x', 'y');
    mochito.verify(mock, mochito.once()).doSomething('z', 'y');
    mochito.verify(mock, mochito.times(2)).doSomething(mochito.anything(), 'y');
  });
});

Global Functions

If you really want the JsHamcrest & JsMockito functions to be available globally, you can simply write:

mochito.installTo(global);