Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chai-sinon

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-sinon

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

  • 2.8.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
513
decreased by-33.98%
Maintainers
1
Weekly downloads
 
Created
Source

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.assertCalledWith(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/methodSinon–Chai assertion
calledspy.should.have.been.called
callCountspy.should.have.callCount(n)
calledOncespy.should.have.been.calledOnce
calledTwicespy.should.have.been.calledTwice
calledThricespy.should.have.been.calledThrice
calledBeforespy1.should.have.been.calledBefore(spy2)
calledAfterspy1.should.have.been.calledAfter(spy2)
calledWithNewspy.should.have.been.calledWithNew
alwaysCalledWithNewspy.should.always.have.been.calledWithNew
calledOnspy.should.have.been.calledOn(context)
alwaysCalledOnspy.should.always.have.been.calledOn(context)
calledWithspy.should.have.been.calledWith(...args)
alwaysCalledWithspy.should.always.have.been.calledWith(...args)
calledWithExactlyspy.should.have.been.calledWithExactly(...args)
alwaysCalledWithExactlyspy.should.always.have.been.calledWithExactly(...args)
calledWithMatchspy.should.have.been.calledWithMatch(...args)
alwaysCalledWithMatchspy.should.always.have.been.calledWithMatch(...args)
returnedspy.should.have.returned(returnVal)
alwaysReturnedspy.should.have.always.returned(returnVal)
threwspy.should.have.thrown(errorObjOrErrorTypeStringOrNothing)
alwaysThrewspy.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 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!

Keywords

FAQs

Package last updated on 29 Nov 2016

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