should.jslocomotivejssupertest

How to start Locomotive server/app for integration testing?


I am converting an Express app to Locomotive and I cannot figure out how to migrate my tests.

In Express, I simply did this in my /test/test.api.organization.js file:

var app = require("../app").app,
  request = require("supertest");
  should = require("should");

describe("Organization API", function() {
  it( "GET /api/v1/organizations should return status 200 and Content-Type: application/json.", function (done) {
    postReq.done( function () {
      request(app)
        .get( "/api/v1/organizations" )
        .set( "Authorization", authData )
        .expect( 200 )
        .expect( 'Content-Type', /application\/json/, done );
    });
  });
}

And that was it - simply require'ing the app file was enough. But Locomotive does not have an app.js or server.js file, the server is started simply from command line with lcm server.

How can I start the server/app and make my tests work again?


Solution

  • You can boot the Locomotive app yourself:

    var locomotive = require('locomotive');
    
    describe('Test Name', function() {
      before(function(done) {
        // instantiate locomotive app
        this.app = new locomotive.Locomotive();
        this.app.boot(ROOTDIRECTORY, ENVIRONMENT, function() {
          done();
        });
      });
      // your test cases
    });
    

    ROOT_DIRECTORY is the directory where your Locomotive project lives, ENVIRONMENT is the environment in which you want your Locomotive app to run (usually test).