ionic-team / ionic-app-lib

The library used for using ionic apps - consumed by the CLI and the GUI
44 stars 79 forks source link

Problem calling Serve.stopServer after Serve.start for automated tests #113

Open pguatura opened 8 years ago

pguatura commented 8 years ago

Hi!

I was trying to use the ionic serve feature to execute some automated tests on my app using the following:

//some code
var promise = Q();
  return promise
 .then(function() {
      return Serve.start(options);
})
.then(function(){
        //execute some tests
})
.finally(function(){
        return Serve.stopServer();
});

But, I was getting an error stating that the server was not running. Looking at the code of server.js I found out that when Serve.start is called, it doesn't wait for the server to really start before returning, so we had no garantee that the server would be running when Server.stop is called.

Therefore, I propose the following solution:


Serve.startServer = function startServer(options, app) {
// original code

  // Listen
  var q = Q.defer();
  app.use(server);
  try {
    runningServer = app.listen(options.port, options.address,function(){
        logging.logger.info('Running dev server: '.green.bold, options.devServer);
        q.resolve()
    });
  }catch(ex) {
    q.reject(Utils.fail('Failed to start the Ionic server: ', ex.message));
  }

  return q.promise;
}

WHat do you think?