lundi 30 mars 2015

How to set up code coverage and unit tests for express functions?

I have a route defined like


app.post '/v1/media', authentication.hasValidApiKey, multipart, mediaController.create, mediaController.get


I want to write tests for the individual components of the route. So starting with authentication.hasValidApiKey, that's a function defined in another file:



exports.hasTokenOrApi = (req, res, next) ->
if not req.headers.authorization
return res.status(403).end()

doOtherStuff...


In my test, I have:



authentication = require '../src/middlewares/authentication'

describe 'Authentication Middleware', ->
before (done) ->
done()

it 'should check for authentication', (done) ->
mock_req = null
mock_res = null
mock_next = null

authentication.hasTokenOrApi mock_res, mock_req, mock_next
done()


How do I deal with the req, res and next? And how can I setup code coverage to run? I am running my tests with: export NODE_ENV=test && ./node_modules/.bin/mocha --compilers coffee:'./node_modules/coffee-script/lib/coffee-script/register'


Aucun commentaire:

Enregistrer un commentaire