Simple request and response mock objects to pass into Express routes when testing using Sinon.
npm install sinon-express-mockSimple request and response mock objects to pass into Express routes when testing using Sinon.
The mock objects attach Sinon spys to request methods. See src/index.js for a full list of stubbed out methods.
``shell`
npm install --save-dev sinon-express-mock sinon
Depends on:
* Node v4+ (or Object.assign support needed)
* Sinon
Contents of src/foo.js:
`js`
export default (req, res) => {
res.json({ foo: req.body.foo })
}
Contents of test/foo-test.js:
`js
import route from '../src/foo'
import chai, { expect } from 'chai'
import sinonChai from 'sinon-chai'
import { mockReq, mockRes } from 'sinon-express-mock'
chai.use(sinonChai);
describe('my route', () => {
it('should foo the bar', () => {
const request = {
body: {
foo: 'bar',
},
}
const req = mockReq(request)
const res = mockRes()
route(req, res)
expect(res.json).to.be.calledWith({ foo: request.body.foo })
})
})
`
* res.write() is now stubbed.
* Make sinon a peerDependency`.
* Bundle fix from #3
* Changelog didn't exist! 😱
Dana Woodman and contributors
MIT