Testing with server that's already running.

10 views
Skip to first unread message

Jef Harkay

unread,
Sep 30, 2014, 5:41:50 PM9/30/14
to compo...@googlegroups.com
I'm curious to know if there's a way to run npm test by using a server that's already running.  The init.js file that's created by default creates a new server and uses that instance with supertest's request, but I'd like to essentially use my running instance or somehow call the server.js file to create my server.  The reason being that I have some global variables that get created in server.js, and I'd rather not reproduce the logic in there.

Jef Harkay

unread,
Oct 3, 2014, 5:44:06 PM10/3/14
to compo...@googlegroups.com
I figured out how to do this, and here's my init.js file:

require('should');
var request = require('supertest');

global.getApp = function(done) {
 
if (!process.env.UNIT_TEST_HOST) {
    process
.env.UNIT_TEST_HOST = 'http://127.0.0.1:3005';
 
}
 
var app = request(process.env.UNIT_TEST_HOST);
 
return {
    app
: app,
    baseParams
: {
     
Cookie: 'myCookie=blah'
   
}
 
};
};

And then in each unit test, I call getApp(), set my local request var to getApp().app, and use it like I normally would (without having to specify the app in parens when using request).
Reply all
Reply to author
Forward
0 new messages