How to start Locomotive server/app for integration

2019-09-06 06:12发布

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?

1条回答
2楼-- · 2019-09-06 06:34

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

查看更多
登录 后发表回答