common express testing assertions

Usage no npm install needed!

<script type="module">
  import expectExpress from 'https://cdn.skypack.dev/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');
  • define your system under test
function responseMiddlware(req, res, next) {
    res.status(200).json({success: true});
function namedMiddleware(req, res, 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