node.js - How to test express config? -


i've been trying figure out how test configuration express server (ex. middleware, etc.). haven't been able find examples, , i'm unsure if best way test match expected list of middleware actual list of middleware, else entirely, or if it's config shouldn't tested @ all.

i should add i'm not interested in how it, rather more in higher level concept. i'm using mocha , supertest if helps.

edit: sorry, should have been more clear. i'm trying test file adds & configures middleware. not middleware itself.

it's not clear wish test:

  • if need test existing middleware plugin, provide tests part of distribution. node-modules/module-name directory, invoke npm test.

  • if wish test own middleware, you'll need write tests yourself, perhaps using tests supplied in existing middleware examples.

  • if wish test middleware plugins configured in express, there none unless explicitly add them in express instance.

  • if wish test request through middleware chain, can add own middleware spies request chain need them.

and finally, if wish test express itself, there ample tests in test directory, there in express middleware distributions, 1 of selling points node: of modules provide source explore.

updated

thanks clarification.

since express' app.use() throw exception if call no arguments or pass either undefined argument or null, can pretty sure function pass added middleware processing cascade.

so, assuming:

var express=require('express'),     app=express(),     undefinedvariable; 

calling of:

app.use(); app.use(undefinedvariable); app.use(null); 

will fail with:

.../node-modules/express/lib/application.js:209 throw new typeerror('app.use() requires middleware functions');       ^ typeerror: app.use() requires middleware functions     @ eventemitter.use (.../node-modules/express/lib/application.js:209:11)     @ object.<anonymous> (.../index.js:7:5)     @ module._compile (module.js:456:26)     @ object.module._extensions..js (module.js:474:10)     @ module.load (module.js:356:32)     @ function.module._load (module.js:312:12)     @ function.module.runmain (module.js:497:10)     @ startup (node.js:119:16)     @ node.js:906:3 

now, if really want test if middleware function has been added request processing cascade, you'll need check known side-effect of calling function, in:

var util = require('util'),     assert = require('assert'),     express = require('express');  // define helper functions 

for purposes of explanation, we'll use custom middleware simulate standard express middleware. (this function adds new value express' standard request object. new value "side-effect" testing.)

/**  * custom middleware sets req.hascustommiddleware true.  *  * @param {object} req - express-style request  * @param {object} res - express-style response  * @param {function} next - express middleware callback  */ var custommiddleware = function(req,res,next){   req.hascustommiddleware = true;   next(); }; 

next, factory function throw exception (via assert) supplied msg if test function not return true. (we'll use factory shorthand way automatically create type of testing middleware in well-defined manner.)

/**  * creates new middleware function asserts test.  *  * @param {string} msg - msg use if/when assertion fails  * @param {function} test - function tests expected values.  * @returns {function}  */ var middlewareassertionfactory = function(msg, testfunc) {   assert('string' === typeof msg && msg);   assert('function' === typeof testfunc);   return function(req, res, next) {     assert(testfunc(req, res), util.format('"%s" middleware assertion failed', msg));     next();   }; }; 

now need function assure side-effect value in we're interested exists , has expected value.

/**  * test function expected value(s).  *  * @param {object} req - express-style request  * @param {object} res - express-style response  * @returns {boolean} - true if expected values exist  */ var expectedvaluetestfunction = function(req, res) {   return 'object' === typeof req && (req.hascustommiddleware ? true : false); }; 

which use create middleware testing function passing our middlewareassertionfactory function.

var custommiddlewaretester = middlewareassertionfactory('req.hascustommiddleware exists , true', expectedvaluetestfunction); 

finally configure our express app use our custommiddleware , custommiddlewaretester functions (which must added in order), add simple default route , start our server.

// config express app  var app = express();  // add our custom middleware app.use(custommiddleware);  // , our test expected side-effect app.use(custommiddlewaretester);  // add simple route  app.get('/', function(req, res) {   res.send('hello world!'); });  // fire server  var server = app.listen(3000, function() {   var addr = server.address();   console.log('app listening @ http://%s:%s', addr.address, addr.port); }); 

with server running, request http://localhost:3000/ return

hello world! 

but if change our test function return false simulate case our expected value doesn't exist or not have expected value, in:

var expectedvaluetestfunction = function(req, res) {   return false; }; 

and make same request, we'll see:

assertionerror: "req.hascustommiddleware exists , true" middleware assertion failed   @ .../index.js:42:9   @ layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)   @ trim_prefix (.../node_modules/express/lib/router/index.js:312:13)   @ .../node_modules/express/lib/router/index.js:280:7   @ function.process_params (.../node_modules/express/lib/router/index.js:330:12)   @ next (.../node_modules/express/lib/router/index.js:271:10)   @ custommiddleware (.../index.js:16:7)   @ layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)   @ trim_prefix (.../node_modules/express/lib/router/index.js:312:13)   @ .../node_modules/express/lib/router/index.js:280:7 

using strategy, should able construct tests express middleware causes change either standard request or response objects, believe include (if not all) of them.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -