Node.js 每次超过超时前都要喝摩卡

Node.js 每次超过超时前都要喝摩卡,node.js,express,mocha.js,chai,Node.js,Express,Mocha.js,Chai,我正在尝试为使用Express和MongoDB制作的RESTAPI设置测试。我想使用mocha,chai和chai http但是我得到了一个奇怪的行为,似乎每个函数之前的都超过了超时时间,好像它从来没有被解决过。我怎样才能解决这个问题 //During the test the env variable is set to test process.env.NODE_ENV = 'test'; let mongoose = require("mongoose"); let User = req

我正在尝试为使用Express和MongoDB制作的RESTAPI设置测试。我想使用
mocha
chai
chai http
但是我得到了一个奇怪的行为,似乎每个
函数之前的
都超过了超时时间,好像它从来没有被解决过。我怎样才能解决这个问题

//During the test the env variable is set to test
process.env.NODE_ENV = 'test';

let mongoose = require("mongoose");
let User = require('../models/User');

//Require the dev-dependencies
let chai = require('chai');
let chaiHttp = require('chai-http');
let app = require('../app');
let should = chai.should();

chai.use(chaiHttp);
//Our parent block
describe('Users', function () {
  beforeEach(function (done) { //Before each test we empty the database
    User.remove({}, function (err) { 
      done();         
    });     
  });
  /*
   * Test the /GET route
   */
  describe('/GET users', function () {
    it('it should GET all the users', function (done) {
      chai.request(app)
        .get('/users')
        .end(function (err, res) {
            res.should.have.status(200);
            res.body.should.be.a('array');
            res.body.length.should.be.eql(0);
          done();
        });
    });
  });

});

假设您的连接正常工作,并且只是超时问题,则需要使用this.timeout()函数将超时时间延长到默认值(2000ms)之外。例如,在beforeach函数中添加此.timeout(10000)会将超时设置为10秒,这将给用户.remove调用更多的时间来完成


下面是此SO答案中的一个示例:

您确定正确配置了与数据库的连接吗?当您调用
用户时,连接打开。删除
?什么是
删除
方法?我在中找不到
remove
方法。所以你可能已经实现了它。我的猜测是
remove
方法从未调用回调函数,因此
done
从未被调用,因此超时。PS:如果您希望删除数据库中的所有条目,可以使用