Search code examples
node.jsexpresstestingchaisupertest

Express API Integration testing: Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test


I am creating integration tests for my api and have run into the following error:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test

I know this question has been asked a couple of times but the answers haven't helped me solve this. The test in question is to test a POST route, and the done callback is being called:

it('should create a transaction', function(done) {
    request(app)
      .post('/api/transactions')
      .send({
        name: 'Cup of coffee',
        amount: 2.50,
        date: '2016-11-17T17:08:45.767Z'
      })
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(201)
      .end(function(err, resp) {
        expect(resp.body).to.be.an('object');
        done();
      })
  })

The post route is as follows:

.post(function (req, res) {
    var transaction = new Transaction()
    transaction.name = req.body.name
    transaction.amount = req.body.amount
    transaction.date = req.body.date

    transaction.save(function (err) {
      if (err) {
        res.send(err)
      }
      res.json(transaction)
    })
  })

The Mongoose Schema for the transaction is:

var mongoose = require('mongoose')
var Schema = mongoose.Schema

var TransactionsSchema = new Schema({
  name: String,
  amount: Number,
  date: { type: Date, default: Date.now }
}, {
  collection: 'transactions'
})

module.exports = mongoose.model('Transactions', TransactionsSchema)

Any ideas? Thank you :)


Solution

  • Inside your test you can specify the test timeout

    it('should create a transaction', function(done) {
        // Specify a timeout for this test
        this.timeout(30000);
    
        request(app)
          .post('/api/transactions')
          .send({
            name: 'Cup of coffee',
            amount: 2.50,
            date: '2016-11-17T17:08:45.767Z'
          })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(201)
          .end(function(err, resp) {
            expect(resp.body).to.be.an('object');
            done();
          })
      });