Reputation: 581
I have multiple test files. Each file has a before
and after
functions, doing server startup,shutdown and db cleaning activities.
Structure of a sample test file:
describe('some test:', function() {
before('reset db; start server', start_server)
after('close server', close_server)
// some tests
describe('#clear_expired_signals:', clear_expired_signals)
describe('#delete_signal ', delete_signal)
}
I am getting EADDRINUSE
exception most of the times when these files are executed together because the server is being started in the same port during each before
call.
Is it possible to force mochajs to execute single file at a time ( file order is not a concern) ?
Edit:
before function sample
function start_server(done) {
intialize_server.start().then(function(options) {
seneca = options.seneca;
seneca.client({
host: 'localhost',
port:options.port,
});
done();
}
}
function intialize_server(){
return new Promise(function (resolve, reject) {
seneca.ready(function () {
seneca.listen({
host: 'localhost',
port: custom_port,
});
resolve({
seneca: seneca,
port: custom_port
});
// console.log('test server listening')
});
});
}
after function sample
function close_server(done) {
console.log('closing seneca instance');
seneca.close(done);
}
Upvotes: 3
Views: 1765
Reputation: 581
I got around this problem. Instead of running before
and after
for each test file, I have added ROOT-LEVEL HOOKS
as given in the documentation
ROOT-LEVEL HOOKS
You may also pick any file and add “root”-level hooks. For example, add beforeEach() outside of all describe() blocks. This will cause the callback to beforeEach() to run before any test case, regardless of the file it lives in (this is because Mocha has an implied describe() block, called the “root suite”).
Upvotes: 1