Extends Chai with assertions for the Sinon.JS mocking framework.
Installations
npm install sinon-chai
Developer Guide
Typescript
No
Module System
ESM
Node Version
22.5.1
NPM Version
10.8.2
Score
98.5
Supply Chain
98.9
Quality
81.1
Maintenance
100
Vulnerability
99.3
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
chaijs
Download Statistics
Total Downloads
247,971,963
Last Day
138,176
Last Week
626,036
Last Month
2,776,522
Last Year
37,056,148
GitHub Statistics
1,090 Stars
155 Commits
106 Forks
22 Watching
2 Branches
37 Contributors
Bundle Size
2.73 kB
Minified
0.98 kB
Minified + Gzipped
Package Meta Information
Latest Version
4.0.0
Package Id
sinon-chai@4.0.0
Unpacked Size
15.64 kB
Size
5.02 kB
File Count
4
NPM Version
10.8.2
Node Version
22.5.1
Publised On
25 Jul 2024
Total Downloads
Cumulative downloads
Total Downloads
247,971,963
Last day
2.1%
138,176
Compared to previous day
Last week
-5.8%
626,036
Compared to previous week
Last month
1.3%
2,776,522
Compared to previous month
Last year
-7.5%
37,056,148
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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:
1sinon.assert.calledWith(mySpy, "foo");
or awkwardly trying to use Chai's should
or expect
interfaces on spy properties:
1mySpy.calledWith("foo").should.be.ok; 2expect(mySpy.calledWith("foo")).to.be.ok;
you can say
1mySpy.should.have.been.calledWith("foo"); 2expect(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
. Similarly, note that the always
methods are accessed with Chai's .always
prefix; should.have.been.alwaysCalledWith
will not work - instead, use should.always.have.been.calledWith
.
For simplicity, this library intentionally only implements Sinon's spy methods, and does not add an interface for Sinon.assert.match
. Sinon's matchers are implemented by the samsam
library, so if you want a should/expect interface to assert.match
you may be interested in chai-samsam, which adds a .deep.match
verb that will work with Sinon matchers.
For assert
interface there is no need for sinon-chai
or chai-samsam
. You can install Sinon.JS assertions right into Chai's assert
object with expose
:
1var chai = require("chai"); 2var sinon = require("sinon"); 3 4sinon.assert.expose(chai.assert, { prefix: "" });
Examples
Using Chai's should
:
1"use strict"; 2var chai = require("chai"); 3var sinon = require("sinon"); 4var sinonChai = require("sinon-chai"); 5chai.should(); 6chai.use(sinonChai); 7 8function hello(name, cb) { 9 cb("hello " + name); 10} 11 12describe("hello", function () { 13 it("should call callback with correct greeting", function () { 14 var cb = sinon.spy(); 15 16 hello("foo", cb); 17 18 cb.should.have.been.calledWith("hello foo"); 19 }); 20});
Using Chai's expect
:
1"use strict"; 2var chai = require("chai"); 3var sinon = require("sinon"); 4var sinonChai = require("sinon-chai"); 5var expect = chai.expect; 6chai.use(sinonChai); 7 8function hello(name, cb) { 9 cb("hello " + name); 10} 11 12describe("hello", function () { 13 it("should call callback with correct greeting", function () { 14 var cb = sinon.spy(); 15 16 hello("foo", cb); 17 18 expect(cb).to.have.been.calledWith("hello foo"); 19 }); 20});
Installation and Usage
Node
Do an npm install --save-dev sinon-chai
to get up and running. Then:
1var chai = require("chai"); 2var sinonChai = require("sinon-chai"); 3 4chai.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:
1define(function (require, exports, module) { 2 var chai = require("chai"); 3 var sinonChai = require("sinon-chai"); 4 5 chai.use(sinonChai); 6});
<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:
1<script src="chai.js"></script> 2<script src="sinon-chai.js"></script> 3<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!
No vulnerabilities found.
No security vulnerabilities found.