Using Superagent/Supertest with Express app in Mocha tests
Asked Answered
O

1

9

I am attempting to write tests for the REST API that I am developing, using Mocha. I discovered Superagent which lets me make HTTP requests. I am using an Express app, which I pass to Superagent, but I get strange errors about Mongoose when trying to run these tests with the Express app passed in this way. Here is my code for the tests:

var
    // Node modules
    mongoose = require('mongoose')
  , async = require('async')
  , should = require('should')
  , request = require('superagent')

  , app = require('../../app_mocked')
  , Link = mongoose.model('Link')

request = request(app)

describe('Links resource', function () {

  var userId = '50dc81654dca01006b000005'
    , linkId
    , sampleLink = {
        'uri': 'http://test.com/',
        'meta': {
          'title': 'Test',
          'desc': 'Test link desc'
        },
        'shares': [{
          'uid': userId,
          'date': new Date(),
          'message': 'Test link message'
        }]
      }

  it('POST /api/users/:id/links', function (done) {
    request(app).post('/api/users/' + userId + '/links')
      .send(sampleLink)
      .end(function (err, res) {
        res.should.have.status(200)
        res.body.should.have.property('id')

        linkId = res.body.id

        done()
      })
  })

  it('GET /api/users/:id/links', function (done) {
    request(app).get('/api/users/50dc81654dca01006b000005/links')
      .end(function (err, res) {
        res.should.have.status(200)
        res.body.should.have.lengthOf(1)

        done()
      })
  })

})

The error I get is this:

1) Links resource POST /api/users/:id/links:
   TypeError: Cannot call method 'update' of null
    at MongoStore.MONGOSTORE.set (/Users/Oliver/Development/Personal/Reader/node_modules/connect-mongodb/lib/connect-mongodb.js:146:15)
    at Session.save (/Users/Oliver/Development/Personal/Reader/node_modules/express/node_modules/connect/lib/middleware/session/session.js:63:25)
    at ServerResponse.res.end (/Users/Oliver/Development/Personal/Reader/node_modules/express/node_modules/connect/lib/middleware/session.js:280:19)
    at ServerResponse.res.send (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/response.js:149:8)
    at ServerResponse.res.json (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/response.js:191:15)
    at ServerResponse.res.send (/Users/Oliver/Development/Personal/Reader/node_modules/express/lib/response.js:117:21)
    at Promise.exports.create (/Users/Oliver/Development/Personal/Reader/server/resources/links.js:29:9)

However, this error only appears sometimes. 1/5 times, the tests will pass with no problem. This makes me think that the tests are sometimes running before app has fully loaded.

Alternatively, if I run the app in a separate session and just pass the URL to request, like below, then it works every time:

request = request('http://localhost:3000')

Is this the reason why? If so, how can I only run the tests once app has fully loaded?

Orit answered 7/2, 2013 at 22:40 Comment(0)
O
10

Turns out that I had to specify a before test to wait for the MongoDB connection to open before running the tests.

before(function (done) {
  mongoose.connection.on('open', done)
})
Orit answered 7/2, 2013 at 22:52 Comment(1)
I think it's also a good practice for the express server to not start listening until the db connections are all madeAuston

© 2022 - 2024 — McMap. All rights reserved.