I'm using mocha.js and supertest.js to test the requests of my json server on express.js. These are my imports:
request = require('supertest')
assert = require('assert') # Node assert
app = require('../app') # Vanilla express app
This is my request implementation in the express app:
app.get '/user/:id', (req, res) ->
res.json {}
and this is my test:
describe 'GET /user/:id', ->
it 'should return the user data if user found', (done) ->
request(app)
.get("/user/some_id")
.end((err, res) ->
assert.equal('test', 'test')
done()
)
This works, but if I change my request to:
app.get '/user/:id', (req, res) ->
User.findById req.param('id'), (err, doc) ->
res.json {}
the mocha test just times out. I'm guessing this has something to do with the fact that the find is async and the test doesn't wait for it to finish. How do I solve this?
Try to increase the timeout:
mocha --timeout 5000
The default is 2000 ms and might be too short. From the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With