Loopback and mocha: wait for the server to finish loading scripts

Hello as the title of the question. I was wondering how to check if the feedback scripts had finished before running the tests. In the example project:

https://github.com/strongloop/loopback-example-relations

there is a file in the test folder that seems to do the job, but unfortunately does not solve it.

start-server.js:

var app = require('../server/server');

module.exports = function(done) {
  if (app.loaded) {
    app.once('started', done);
    app.start();
  } else {
    app.once('loaded', function() {
      app.once('started', done);
      app.start();
    });
  }
};

This script is loaded into the rest of the test APIs as follows:

before(function(done) {
    require('./start-server');
    done();
});

but the function is never called. Is this the right way to use this script?

I ended up with the following implementation:

before(function (done) {
    if (app.booting) {
        console.log('Waiting for app boot...');
        app.on('booted', done);
    } else {
        done();
    }
});

which works, but I was puzzled by the server startup script.

EDIT following @stalin's advice. I changed the function beforeas follows:

before(function(done) {
    require('./start-server')(done);
});

else done .

+6
3

start-server script. :

before(function(done) {
    var server = require('./start-server');
    server(done);
});
+4

, , (, ). booting = false , :

// boot script with extra callback param:
module.exports = function (app, cb) {
  var db = app.dataSources.db;

  // update all database models
  db.autoupdate(function (err) {
    if (err) throw err;
    cb();
  });
};
+1

, Loopback

https://github.com/strongloop/loopback/blob/44951a1032d2115a20a098fbeea767e0a5cd72c1/test/helpers/loopback-testing-helper.js#L39

beforeEach(function(done) {
    this.app = app;
    var _request = this.request = request(app);
    this.post = _request.post;
    this.get = _request.get;
    this.put = _request.put;
    this.del = _request.del;
    this.patch = _request.patch;

    if (app.booting) {
      return app.once('booted', done);
    }
done();

,

https://github.com/strongloop/loopback/blob/b77907ffa59c7031fcb3bb6dbff96894bc597ba4/test/user.integration.js#L16

describe('access control - integration', function() {
  lt.beforeEach.withApp(app);
0
source

All Articles