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

sinon-chai-es

v3.7.0

Published

Extends Chai with assertions for the Sinon.JS mocking framework.

Downloads

38

Readme

Sinon.JS Assertions for Chai

Sinon–Chai provides a set of custom assertions for using the Sinon.JS spy, stub, and mocking framework with the Chai assertion library. You get all the benefits of Chai with all the powerful tools of Sinon.JS.

Instead of using Sinon.JS's assertions:

sinon.assert.calledWith(mySpy, "foo");

or awkwardly trying to use Chai's should or expect interfaces on spy properties:

mySpy.calledWith("foo").should.be.ok;
expect(mySpy.calledWith("foo")).to.be.ok;

you can say

mySpy.should.have.been.calledWith("foo");
expect(mySpy).to.have.been.calledWith("foo");

Assertions

All of your favorite Sinon.JS assertions made their way into Sinon–Chai. We show the should syntax here; the expect equivalent is also available.

| Sinon.JS property/method | Sinon–Chai assertion | |--------------------------|-------------------------------------------------------------------| | called | spy.should.have.been.called | | callCount | spy.should.have.callCount(n) | | calledOnce | spy.should.have.been.calledOnce | | calledTwice | spy.should.have.been.calledTwice | | calledThrice | spy.should.have.been.calledThrice | | calledBefore | spy1.should.have.been.calledBefore(spy2) | | calledAfter | spy1.should.have.been.calledAfter(spy2) | | calledImmediatelyBefore | spy.should.have.been.calledImmediatelyBefore(spy2) | | calledImmediatelyAfter | spy.should.have.been.calledImmediatelyAfter(spy2) | | calledWithNew | spy.should.have.been.calledWithNew | | alwaysCalledWithNew | spy.should.always.have.been.calledWithNew | | calledOn | spy.should.have.been.calledOn(context) | | alwaysCalledOn | spy.should.always.have.been.calledOn(context) | | calledWith | spy.should.have.been.calledWith(...args) | | alwaysCalledWith | spy.should.always.have.been.calledWith(...args) | | calledOnceWith | spy.should.always.have.been.calledOnceWith(...args) | | calledWithExactly | spy.should.have.been.calledWithExactly(...args) | | alwaysCalledWithExactly | spy.should.always.have.been.calledWithExactly(...args) | | calledOnceWithExactly | spy.should.always.have.been.calledOnceWithExactly(...args) | | calledWithMatch | spy.should.have.been.calledWithMatch(...args) | | alwaysCalledWithMatch | spy.should.always.have.been.calledWithMatch(...args) | | returned | spy.should.have.returned(returnVal) | | alwaysReturned | spy.should.have.always.returned(returnVal) | | threw | spy.should.have.thrown(errorObjOrErrorTypeStringOrNothing) | | alwaysThrew | spy.should.have.always.thrown(errorObjOrErrorTypeStringOrNothing) |

For more information on the behavior of each assertion, see the documentation for the corresponding spy methods. These of course work on not only spies, but individual spy calls, stubs, and mocks as well.

Note that you can negate any assertion with Chai's .not. E. g. for notCalled use spy.should.have.not.been.called.

For assert interface there is no need for this library. You can install Sinon.JS assertions right into Chai's assert object with expose:

var chai = require("chai");
var sinon = require("sinon");

sinon.assert.expose(chai.assert, { prefix: "" });

Examples

Using Chai's should:

"use strict";
var chai = require("chai");
var sinon = require("sinon");
var sinonChai = require("sinon-chai");
chai.should();
chai.use(sinonChai);

function hello(name, cb) {
    cb("hello " + name);
}

describe("hello", function () {
    it("should call callback with correct greeting", function () {
        var cb = sinon.spy();

        hello("foo", cb);

        cb.should.have.been.calledWith("hello foo");
    });
});

Using Chai's expect:

"use strict";
var chai = require("chai");
var sinon = require("sinon");
var sinonChai = require("sinon-chai");
var expect = chai.expect;
chai.use(sinonChai);

function hello(name, cb) {
    cb("hello " + name);
}

describe("hello", function () {
    it("should call callback with correct greeting", function () {
        var cb = sinon.spy();

        hello("foo", cb);

        expect(cb).to.have.been.calledWith("hello foo");
    });
});

Installation and Usage

Node

Do an npm install --save-dev sinon-chai to get up and running. Then:

var chai = require("chai");
var sinonChai = require("sinon-chai");

chai.use(sinonChai);

You can of course put this code in a common test fixture file; for an example using Mocha, see the Sinon–Chai tests themselves.

AMD

Sinon–Chai supports being used as an AMD module, registering itself anonymously (just like Chai). So, assuming you have configured your loader to map the Chai and Sinon–Chai files to the respective module IDs "chai" and "sinon-chai", you can use them as follows:

define(function (require, exports, module) {
    var chai = require("chai");
    var sinonChai = require("sinon-chai");

    chai.use(sinonChai);
});

<script> tag

If you include Sinon–Chai directly with a <script> tag, after the one for Chai itself, then it will automatically plug in to Chai and be ready for use. Note that you'll want to get the latest browser build of Sinon.JS as well:

<script src="chai.js"></script>
<script src="sinon-chai.js"></script>
<script src="sinon.js"></script>

Ruby on Rails

Thanks to Cymen Vig, there's now a Ruby gem of Sinon–Chai that integrates it with the Rails asset pipeline!