moxpress v0.0.3
moxpress
Useful mocks for unit testing Node.js's Express.
This is an actively-under-development utility that we use internally at Webonise Lab. It enables us to write tests for our Express controller code that executes quickly and gives nice, precise errors.
Usage
Install
npm install --save-dev moxpressUse
// Perform the import
var mockFactory = require("moxpress");
var mocks = mockFactory();
// Get your hands on the relevant variables
var app = mocks.app; // Mock Express "app"
var req = mocks.req; // Mock Express "req"
var res = mocks.res; // Mock Express "res"
// Execute your test
var ctrl = require("./my/controller"); // Load your controller
ctrl.index(req, res); // Call the method
// Assert awaySupported APIs
app
setgetenabledisableenableddisabledlisten(does nothing)- HTTP verb methods
allroute
To retrieve the routes that were created through all and the HTTP verb methods,
use the plural of the HTTP verb (OPTIONS => optionses), which will be an object
whose keys are paths and whose values are callbacks.
req
paramsparamquerycookiessignedCookiesheadersget(including lowercasing the argument and aliasing 'referrer' to 'referer')header(alias ofget)ip(defaults to127.0.0.1)ips(defaults to list with single element127.0.0.1)host(defaults tolocalhost)fresh(defaults totrue)stale(defaults tofalse)xhr(defaults totrue)protocol(defaults tohttp)secure(defaults tofalse)subdomains(defaults to empty array)originalUrl(defaults to empty string)
res
statusCode(holds thestatusvalue)statusset(both object and string versions)header(alias ofset)get(including lowercasing the argument)cookieclearCookiecookies(provides access to cookies as a map ofnameonto[value,options]lists)redirectredirectUrl(holds the URL whichredirectredirects to)locationlocationUrl(holds the URL whichlocationspecifies)send(in all its various permutations)body(holds the body assigned bysend,json, etc.)jsontype(uses node-mime to do the lookup)formatformatter(holds the value passed toformat)attachmentattachmentFile(holds the value pased toattachmentsendFile(both with and without the callback)download(both with and without the callback)linkslink\_values(holds the values passed tolinks)locals
Contributing
Don't like the fact that some part of the API isn't implemented? Then feel free to contribute! This project uses GitHub Flow, so fork, branch, and then submit your pull request. No reasonable pull turned away!
The interesting file is ./lib/moxpress.js.
Note that the response has access to the request and application objects, and the request has access to the application object, so they can use that upstream logic as part of the mocking logic.