middleware-recorder

0.0.1 • Public • Published

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', 'jef@example.com');
  req.session.user = 'Geert';
  next()
}

var obj = {};
record( middleware ).into( obj );
console.log(obj);

/* Logs the following object */

{
  result: { 
    locals: { 
      email: 'jef@example.com' 
    },
    session: { 
      user: 'Geert' } 
    },
    next: true
  }
}

Tape object

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

Wipe

The wipe method will reset the tape.

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

record( middleware ).into(tape);

tape.wipe()

Compare

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( tape.compare(expected) );

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

Eql

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 = {
  //...
};

tape.eql(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){

  res.foobar('barfoo');

};

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

Readme

Keywords

none

Package Sidebar

Install

npm i middleware-recorder

Weekly Downloads

0

Version

0.0.1

License

none

Last publish

Collaborators

  • enome