Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
express middleware mocha test
var middleware = require('middleware'), // the Middleware you want to test
httpMocks = require('node-mocks-http'), // quickly sets up REQUEST and RESPONSE to be passed into Express Middleware
request = {}, // define REQUEST
response = {} // define RESPONSE
;
describe('Middleware test', function(){
context('Valid arguments are passed', function() {
beforeEach(function(done) {
/*
* before each test, reset the REQUEST and RESPONSE variables
* to be send into the middle ware
**/
request = httpMocks.createRequest({
method: 'GET',
url: '/test/path?myid=312',
query: {
myid: '312'
}
});
response = httpMocks.createResponse();
done(); // call done so that the next test can run
});
it('does something', function(done) {
/*
* Middleware expects to be passed 3 arguments: request, response, and next.
* We are going to be manually passing REQUEST and RESPONSE into the middleware
* and create an function callback for next in which we run our tests
**/
middleware(request, response, function next(error) {
/*
* Usually, we do not pass anything into next except for errors, so because
* in this test we are passing valid data in REQUEST we should not get an
* error to be passed in.
**/
if (error) { throw new Error('Expected not to receive an error'); }
// Other Tests Against request and response
if (!request.didSomething) { throw new Error('Expected something to be done'); }
done(); // call done so we can run the next test
}); // close middleware
}); // close it
}); // close context
}); // close describe
/*
* Simple Middleware to set a variable in request.
**/
module.exports = function(request, response, next) {
/*
* Do something to REQUEST or RESPONSE
**/
if (!request.didSomething) {
request.didSomething = true;
next();
} else {
// Something went wrong, throw and error
var error = new Error();
error.message = 'Error doing what this does'
next(error);
}
};
@dels07

This comment has been minimized.

Copy link

commented Aug 15, 2019

can you help on how to test middleware that have parameter?

const middleware = param => (request, response, next) {
    /*
     * Do something to REQUEST or RESPONSE
    **/
    
    if (!request.didSomething) {
        request.didSomething = true;
        next();
    } else {
        // Something went wrong, throw and error
        var error = new Error();
        error.message = 'Error doing what this does'
        next(error);        
    }
};

export default middleware;
@liaodrake

This comment has been minimized.

Copy link
Owner Author

commented Aug 15, 2019

Sure,

Assuming that you do something with the params like this...

/*
 * Simple Middleware to set a variable in request.
**/
module.exports = (params) => (request, response, next) => {
    /*
     * Do something to REQUEST or RESPONSE
    **/

    response.params = params;

    if (!request.didSomething) {
        request.didSomething = true;
        next();
    } else {
        // Something went wrong, throw and error
        var error = new Error();
        error.message = 'Error doing what this does'
        next(error);
    }
};

You can test for it like this

var middleware = require('./middleware'), // the Middleware you want to test
    httpMocks = require('node-mocks-http'), // quickly sets up REQUEST and RESPONSE to be passed into Express Middleware
    request = {}, // define REQUEST
    response = {} // define RESPONSE
;

describe('Middleware test', function(){
    context('Valid arguments are passed', function() {
        beforeEach(function(done) {
            /*
             * before each test, reset the REQUEST and RESPONSE variables
             * to be send into the middle ware
            **/
            request = httpMocks.createRequest({
                method: 'GET',
                url: '/test/path?myid=312',
                query: {
                    myid: '312'
                }
            });
            response = httpMocks.createResponse();

            done(); // call done so that the next test can run
        });

        it('does something', function(done) {
            /*
             * Middleware expects to be passed 3 arguments: request, response, and next.
             * We are going to be manually passing REQUEST and RESPONSE into the middleware
             * and create a function callback for next in which we run our tests
            **/
            const someParams = {
                Foo: "Bar"
            }

            middleware(someParams)(request, response, function next(error) {
                /*
                 * Usually, we do not pass anything into next except for errors, so because
                 * in this test we are passing valid data in REQUEST we should not get an
                 * error to be passed in.
                **/
                if (error) { throw new Error('Expected not to receive an error'); }

                // Other Tests Against request and response
                if (!request.didSomething) { throw new Error('Expected something to be done'); }

                if (!response.params) {
                    throw new Error('Expected params to be added to response');
                }
                if (!response.params.Foo) {
                    throw new Error('Expected params.Foo to be added to response');
                }
                if (!response.params.Foo === Bar) {
                    throw new Error('Expected response.params.Foo === Bar');
                }

                done(); // call done so we can run the next test
            }); // close middleware
        }); // close it
    }); // close context
}); // close describe

The first set of brackets is the outer function of the middle ware, and second set is the original middleware.

Hope that helps,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.