Hi as of title of question I was wondering how one can check if loopback boot scripts have finished before launching tests. In a example project:
there is a file in the test folder that seems to do the job but unfortunately it 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 in the rest test api as follows:
before(function(done) {
require('./start-server');
done();
});
but the function is never invoked. Is this the correct way it was meant to use that script?
I ended 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 that start-server script.
EDIT
following the @stalin advice I modified the before
function as follows:
before(function(done) {
require('./start-server')(done);
});
and the execution goes in the else
branch but done
is never called.
Hi as of title of question I was wondering how one can check if loopback boot scripts have finished before launching tests. In a example project:
https://github./strongloop/loopback-example-relations
there is a file in the test folder that seems to do the job but unfortunately it 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 in the rest test api as follows:
before(function(done) {
require('./start-server');
done();
});
but the function is never invoked. Is this the correct way it was meant to use that script?
I ended 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 that start-server script.
EDIT
following the @stalin advice I modified the before
function as follows:
before(function(done) {
require('./start-server')(done);
});
and the execution goes in the else
branch but done
is never called.
3 Answers
Reset to default 4You are never passing the done function to the start-server
script. Try to do this:
before(function(done) {
var server = require('./start-server');
server(done);
});
It might not work when your boot scripts are using asynchronous functions (for example to automigrate models schema). Application will set booting = false
and not wait for callbacks to finish until you invoke a callback explicitly:
// 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();
});
};
I just want to point out the way Loopback team did it
https://github./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();
Then you'd find that they're calling it pretty much within every integration test
https://github./strongloop/loopback/blob/b77907ffa59c7031fcb3bb6dbff96894bc597ba4/test/user.integration.js#L16
describe('access control - integration', function() {
lt.beforeEach.withApp(app);