danawoodman / sinon-express-mock

Simple request and response mock objects to pass into Express routes when testing using Sinon.

Home Page:https://www.npmjs.com/package/sinon-express-mock

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

chainable functions

skibz opened this issue · comments

suppose i were testing this route handler

// myroute.js
module.exports = function(req, res) {
    return res.status(200).end()
}

with this test case

it('should call end', function() {
    var myroute = require('./myroute')
    var res = mockRes()
    myroute(null, res)
    expect(res.end).to.be.called
})
// TypeError: Cannot read property 'end' of undefined

the type error being thrown is because the stubbed res.status function is a sinon spy and does not return res. could it be possible to alter this library to support chaining function calls in a backwards compatible manner?

i'd be happy to have a go at adding this, just wanted to get your thoughts on the matter 🙏

@skibz totally agree. In fact, I was thinking the same thing the other day! Feel free to submit a PR and I will merge it in 😄

Heads up; ill be out of town for the next two weeks so it would be mid-July before I could review

Looks like #2 updated the src script but not the lib output. In effect, 1.3.0 does not actually have this fix.

@marcind you're totally right! I just pushed v1.3.1 which bundles the change. Thanks for the heads up! 🍻