How to start serverless-offline in mocha test before function

Viewed 218

I'd like to use serverless-offline in some mocha tests. Something like:

describe('run some tests', function() {
  before(function(done) {
    // Start serverless-offline here
    return done();
  });

  after(function(done) {
    // Stop serverless-offline here
    return done();
  });

  it('run the test against serverless-offline', function(done) {
    // test code here
    return done();
  });
});

Is there a way to start serverless-offline programatically rather than via the cli? Or some other way to run tests on serverless functions offline?

1 Answers

What I recommend is use concurrently from npm (or a similar package that allows you to run multiple processes in a single command.

Then add in your before function a loop that waits for the server to spin up by pinging an endpoint with a HTTP client repeatedly. You can add either a default route or a health-check route to do this.

Related