Timeout when running mocha test on mongoose model and express route

有些话、适合烂在心里 提交于 2019-12-11 11:49:54

问题


I'm having an issue writing tests for my Mongoose models and my express app (routes)

I have a very simple app.js file:

var env = process.env.NODE_ENV || 'development',
    express = require('express'),
    config = require('./config/config')[env],
    http = require('http'),
    mongoose = require('mongoose');

// Bootstrap db connection
mongoose.connect(config.db)

// Bootstrap models
var models_path = __dirname + '/app/model';
fs.readdirSync(models_path).forEach(function(file) {
    if (~file.indexOf('.js')) {
        require(models_path + '/' + file);
    }
});

var app = express();

// express settings
require('./config/express')(app, config);

// Bootstrap routes
require('./config/routes')(app, compact);

if (!module.parent) {
    app.listen(app.get('port'), function() {
        console.log('Server started on port ' + app.get('port'));
    })
}

module.exports = app;

I have a folder called model that contains my mongoose models.

I have a test folder, with accountTest.js - looks a bit like this:
(This is to test my Account model)

var utils = require('./utils'),
  should = require('chai').should(),
  Account = require('../app/model/account');

describe('Account', function() {
  var currentUser = null;
  var account = null;

  it('has created date set on save', function(done) {
    var account = new Account();

    account.save(function(err) {
      account.created.should.not.be.null;
      done();
    });
  });

utils is taken from here: http://www.scotchmedia.com/tutorials/express/authentication/1/06

This works, if I leave it at just this one test.

If I add another test, to test my express route, something like this:

var request = require('supertest'),
    app = require('../../app'),
    should = require('chai').should();

describe('Account controller', function() {

    it('GET /account returns view', function(done) {
        //omitted for brevity
        done();
    });
});

Then I get a timeout error, on my tests for my model...

The line that is effecting it is app = require('../../app')
If I remove that, then there's no timeout.

I realise it may be something to do with the mongoose connection, but not sure how to "share" it between tests?


回答1:


mocha has a root Suite:

You may also pick any file and add "root" level hooks, for example add beforeEach() outside of describe()s then the callback will run before any test-case regardless of the file its in. This is because Mocha has a root Suite with no name.

Use that to start your Express server once (and we use an environment variable so that it runs on a different port than our development server):

before(function () {
  process.env.NODE_ENV = 'test';
  require('../../app.js');
});

(We don't need a done() here because require is synchronous.) This was, the server is started exactly once, no matter how many different test files include this root-level before function.



来源:https://stackoverflow.com/questions/20097453/timeout-when-running-mocha-test-on-mongoose-model-and-express-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!