Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js mocha test request

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?

like image 968
Opptatt Jobber Avatar asked Jul 22 '26 12:07

Opptatt Jobber


1 Answers

Try to increase the timeout:

mocha --timeout 5000

The default is 2000 ms and might be too short. From the documentation.

like image 176
zemirco Avatar answered Jul 25 '26 07:07

zemirco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!