danawoodman / sinon-express-mock

Simple request and response mock objects to pass into Express routes when testing using Sinon.
https://www.npmjs.com/package/sinon-express-mock
MIT License
47 stars 16 forks source link

chainable functions #1

Closed skibz closed 7 years ago

skibz commented 8 years ago

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 🙏

danawoodman commented 8 years ago

@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

marcind commented 7 years ago

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

danawoodman commented 7 years ago

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