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
epub helper mock route router sinon test testing util utility

sinon-express-mock

Simple 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.

Install

npm install --save-dev sinon-express-mock sinon

Depends on:

Usage

Contents of src/foo.js:

export default (req, res) => {
  res.json({ foo: req.body.foo })
}

Contents of test/foo-test.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 })
  })
})

Changelog

v2.0.3

v2.0.0

v1.3.1

pre v1.3.1

Credits

Dana Woodman and contributors

License

MIT