node package manager
We need your input. Help make JavaScript better: Take the 2017 JavaScript Ecosystem survey »


Middleware recorder

Installing it

npm install middleware-recorder

What does this package do?

It lets you pass a middleware to a recorder and record the result into an object you can assert.

Basic usage

var record = require('middleware-recorder').middleware;
var middleware = function(req, res, next){
  res.local('email', '');
  req.session.user = 'Geert';
var obj = {};
record( middleware ).into( obj );
/* Logs the following object */
  result: { 
    locals: { 
      email: '' 
    session: { 
      user: 'Geert' } 
    next: true

Tape object

We also provide you with a tape object you can record into that has two methods.


The wipe method will reset the tape.

var tape = require('middleware-recorder').tape;
record( middleware ).into(tape);


To use compare you need to install the optional package difflet.

npm install difflet

This will show you a diff between the result and the expected object.

var tape = require('middleware-recorder').tape;
record( middleware ).into(tape);
var expected = {
console.log( );

This will use console.log to output the difference between the two objects.


This will do a assert.deepEqual between the result and the expected object. It will use compare to output the error message if the assertion fails. ( So for this you also need difflet ) This is usefull when you are testing the complete result instead of small parts of it.

var tape = require('middleware-recorder').tape;
record( middleware ).into(tape);
var expected = {

The request object

If you could only test middleware that doesn't take a meaningful request object this package would kinda suck. So the second argument of recording function takes a request object.

var request = { body: { username: 'Geert' } };
record( middleware, request ).into(tape);

The response object

The response object has a few default methods ( see recorder/response.js ). In case I missed some methods you can either contact me or you can add a third argument to the record function.

var middleware = function(req, res, next){
var extendRes = {
  foobar: function(value){
    this.result.foobar = value;
record( middleware, null, extendRes ).into( tape );

See it in action

Since you will mostly be using this for testing check out the tests/examples at: tests/middleware.js.

Running the tests

npm install -g mocha
make test