express-unit
Express middleware testing made easy.
λ npm install express-unit
Contents
Usage
Express Unit exports a helper function for running a single middleware. To use, just import
it and pass it a setup
function, your middleware
, and an optional callback
.
Parameters
setup
- A function defined to set up the Request/Response lifecycle before it entersmiddleware
. Passnull
if not needed.setup
is called with three arguments:req
- A dummyRequest
object.res
- A dummyResponse
object.next
- A function used to proceed tomiddleware
.
middleware
- The middleware function under test. Passed different arguments depending on whether it is an "error handling" middleware.err
- forwarded fromnext(err)
insetup
if the middleware is an error handler.req
- The dummyRequest
object visited bysetup
.res
- The dummyResponse
object visited bysetup
.next
- A function used to signal the completion of themiddlware
.
callback
- An optional function used to inspect the outcome of passing the Request/Response lifecycle throughsetup
andmiddleware
.err
- Forwarded fromnext(err)
inmiddleware
(if any).req
- The dummyRequest
object visited bysetup
andmiddleware
.res
- The dummyResponse
object visited bysetup
andmiddleware
.
setup
Your setup function will be called with a req
, res
, and next
to prepare the request lifecycle for your middleware. This is your opportunity to set headers on req
or spy/stub any relevant methods on res
. Call next
to execute your middleware.
If for some reason you don't want to supply a setup, just pass null
.
// middleware.js { const token = req if token return const err = 'where is your token?' }
// middleware.test.js
Callback
Express Unit supports callbacks. Pass a callback as the third argument to inspect the results.
// middleware.js { const token = req if token return const err = 'Access token required.' return }
// middleware.test.js
Async/Await
Express Unit also supports async
middleware. This is any middleware that is an async
function or simply returns a Promise
. express-unit
will resolve an array of [err, req, res]
that you can either await
or receive in a call to then
. Works great with express-async-wrap
.
// middleware.js const middleware =
// middleware.test.js
Why Express Unit?
Express Unit puts the "unit" back in unit testing for Express.js apps. It's a small, simple helper for exercising individual middleware functions in isolation. Most testing tutorials and examples for express
apps will utilize supertest
(or similar).
This is great for testing your entire app
or a given router
within your app
. For some endpoints, various middleware functions are put in place to determine the response, e.g. confirming a user's identity, verifying their access rights, and bailing out of a request early if preconditions are not met. But testing all of this in concert is integration testing, which is often best left at testing the "happy path". A single route could employ a middleware stack like this:
router router
There are 4 different middleware functions involved in this single route. At least one of which needs access to some kind of data store. But each middleware is very focused and can be reused or replaced (Yay!).