expect-express

1.2.0 • Public • Published

expect-express

Travis Codecov npm downloads

check common express unit test scenarios with expect

  • setup your imports and configure expect-express
const router = require('express').Router();
const expect = require('expect');
const expectExpress = require('expect-express');
expect.extend(expectExpress);
  • define your system under test
function responseMiddlware(req, res, next) {
    res.status(200).json({success: true});
}
function namedMiddleware(req, res, next) {
    next();
}
 
router.get('/:id', namedMiddleware, (req, res, next) => { next(); }, () => {});
  • test if a route is defined by specifying an HTTP verb and a path
expect(router).toHaveRoute('GET', '/:id');
  • test if a given route has params defined getRoute helper methods facilitates route selection
expect(expectExpress.helper.getRoute(router, 'GET', '/:id')).toHaveRouteParameter('id');
  • test if specific middleware (function, name or anonymous) is defined for given route
expect(expectExpress.helper.getRoute(router, 'GET', '/:id')).toHaveMiddleware(namedMiddleware);
expect(expectExpress.helper.getRoute(router, 'GET', '/:id')).toHaveMiddleware('namedMiddleware');
expect(expectExpress.helper.getRoute(router, 'GET', '/:id')).toHaveMiddleware();
  • test if middleware function returns expected response
const reqDefinition = {
    verb: 'POST',
    url: '/',
    payloadType: 'body',
    payload: { test: 'valid' }
};
expect(responseMiddleware).toRespondWith(reqDefinition, 200, {success: true});
  • test if next was called in middleware
expect(namedMiddleware).toHaveCalledNext(reqDefinition);

Readme

Keywords

Package Sidebar

Install

npm i expect-express

Weekly Downloads

1

Version

1.2.0

License

MIT

Last publish

Collaborators

  • floedelmaier