# 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: ```javascript sinon.assertCalledWith(mySpy, "foo"); ``` or awkwardly trying to use Chai's `should` or `expect` interfaces on spy properties: ```javascript mySpy.calledWith("foo").should.be.ok; expect(mySpy.calledWith("foo")).to.be.ok; ``` you can say ```javascript 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) |
calledWithExactly | spy.should.have.been.calledWithExactly(...args) |
alwaysCalledWithExactly | spy.should.always.have.been.calledWithExactly(...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) |