【问题标题】:Express API Integration testing: Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this testExpress API 集成测试:错误:超过 2000 毫秒的超时。确保在此测试中调用了 done() 回调
【发布时间】:2016-11-18 22:30:40
【问题描述】:

我正在为我的 api 创建集成测试并遇到以下错误:

错误:超过 2000 毫秒的超时。确保 done() 回调正在执行 在这个测试中调用

我知道这个问题已被问过几次,但答案并没有帮助我解决这个问题。有问题的测试是测试一个 POST 路由,并且正在调用 done 回调:

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();
      })
  })

发帖路线如下:

.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)
    })
  })

交易的 Mongoose Schema 是:

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)

有什么想法吗?谢谢你:)

【问题讨论】:

    标签: node.js express testing chai supertest


    【解决方案1】:

    在您的测试中,您可以指定测试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();
          })
      });
    

    【讨论】:

    • 啊伙计,非常酷 - 谢谢。所以只是我的笔记本电脑太旧了,运行测试需要很长时间? :)
    猜你喜欢
    • 1970-01-01
    • 2015-12-16
    • 2017-12-25
    • 2019-07-24
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多