moxpress
v0.0.3
Published
Mock library for unit testing Express applications
Downloads
2
Readme
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 moxpress
Use
// 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 away
Supported APIs
app
set
get
enable
disable
enabled
disabled
listen
(does nothing)- HTTP verb methods
all
route
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
params
param
query
cookies
signedCookies
headers
get
(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 thestatus
value)status
set
(both object and string versions)header
(alias ofset
)get
(including lowercasing the argument)cookie
clearCookie
cookies
(provides access to cookies as a map ofname
onto[value,options]
lists)redirect
redirectUrl
(holds the URL whichredirect
redirects to)location
locationUrl
(holds the URL whichlocation
specifies)send
(in all its various permutations)body
(holds the body assigned bysend
,json
, etc.)json
type
(uses node-mime to do the lookup)format
formatter
(holds the value passed toformat
)attachment
attachmentFile
(holds the value pased toattachment
sendFile
(both with and without the callback)download
(both with and without the callback)links
link\_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.