0

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?

ragulka
  • 4,312
  • 7
  • 48
  • 73

1 Answers1

2

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).

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • I also realized that I need to use `app.express` for supertest instead of just `app`. – ragulka Jan 10 '13 at 07:55
  • 1
    This doesn't work in Locomotive 0.4.1 since the boot() signiture appears to have changed to no longer include allow ```ROOTDIRECTORY```. The only way I've found is to export the server.js as mentioned here: http://stackoverflow.com/questions/18941736/ensuring-express-app-is-running-before-each-mocha-test – Andrew Lank Apr 12 '14 at 15:57