Table of Contents

Sinon JS

If your test will also involve other 3rd party library, etc. multer. You better also check their own test cases for reference. e.g. https://github.com/expressjs/multer/tree/master/test

Reference

Spy

const spy = sinon.spy(object, 'method')
...
spy.restore() // remove spy
expect(spy.callCount).to.be.eql(n) // check if the method is call n times

Stub

Method 1

function fake() {  // a fake function to be called
}

const stub = sinon.stub(object, 'method').callFakes(fake) // you could also replace the fake with another real function
...
stub.restore() // remove stub
expect(stub.callCount).to.be.eql(n) // check if the method is call n times

Method 2

const stub = sinon.stub(object, 'method').returns(val) // always return same val
...
const test = stub()
expect(test).to.be.eql(val)
stub.restore() // remove stub
expect(stub.callCount).to.be.eql(n) // check if the method is call n times

Method 3

const stub = sinon.stub(object, 'method').resolve(val) // always resolve same val
...
const test = await stub()
expect(test).to.be.eql(val)
stub.restore() // remove stub
expect(stub.callCount).to.be.eql(n) // check if the method is call n times