expect-express
v1.2.0
Published
common express testing assertions
Downloads
9
Readme
expect-express
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);