简体   繁体   中英

Unit testing express.js routes

I am new to express and very new to unit testing. Consider the following code:

var express = require('express');
var router = express.Router();

var bookingsController = require ("../controllers/bookings");

router
   .route('/')
   .get(bookingsController.bookings_get)
   .post(bookingsController.bookings_post)


router
   .route('/:id')
   .get(bookingsController.bookings_get_id)
   .put(bookingsController.bookings_put_id)
   .delete(bookingsController.bookings_delete_id)

module.exports = router;

What is the right/recommended way of writing unit tests for this? I would like to be able to test, for example, that router.route('/:id') does not accept POST calls.

I know I can do this with supertest, but I believe that would be considered an integration test as supertest would start the app the run the test.

I've read and tried multiple node.js/express.js testing tutorials but couldn't find an answer to this. Any pointers to existing tutorials most welcome. Direct answers here, even more:)

Here is the unit test solution using sinon.js to stub express.Router function and make assertions.

router.js :

var express = require('express');
var router = express.Router();

var bookingsController = require('./controllers/bookings');

router
  .route('/')
  .get(bookingsController.bookings_get)
  .post(bookingsController.bookings_post);

router
  .route('/:id')
  .get(bookingsController.bookings_get_id)
  .put(bookingsController.bookings_put_id)
  .delete(bookingsController.bookings_delete_id);

module.exports = router;

./controller/bookings.js :

module.exports = {
  bookings_get() {},
  bookings_post() {},
  bookings_get_id() {},
  bookings_put_id() {},
  bookings_delete_id() {},
};

router.test.js :

var sinon = require('sinon');
var express = require('express');

describe('61529619', () => {
  it('should pass', () => {
    const routerStub = {
      route: sinon.stub().returnsThis(),
      post: sinon.stub().returnsThis(),
      get: sinon.stub().returnsThis(),
      put: sinon.stub().returnsThis(),
      delete: sinon.stub().returnsThis(),
    };
    sinon.stub(express, 'Router').callsFake(() => routerStub);
    require('./router');
    sinon.assert.calledWith(routerStub.route, '/');
    sinon.assert.calledWith(routerStub.route, '/:id');
    sinon.assert.calledWith(routerStub.get, sinon.match.func);
    sinon.assert.calledWith(routerStub.post, sinon.match.func);
    sinon.assert.calledWith(routerStub.put, sinon.match.func);
    sinon.assert.calledWith(routerStub.delete, sinon.match.func);
  });
});

unit test results with 100% coverage:

  61529619
    ✓ should pass (869ms)


  1 passing (881ms)

----------------------|---------|----------|---------|---------|-------------------
File                  | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------------------|---------|----------|---------|---------|-------------------
All files             |     100 |      100 |       0 |     100 |                   
 61529619             |     100 |      100 |     100 |     100 |                   
  router.js           |     100 |      100 |     100 |     100 |                   
 61529619/controllers |     100 |      100 |       0 |     100 |                   
  bookings.js         |     100 |      100 |       0 |     100 |                   
----------------------|---------|----------|---------|---------|------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM