• Published 12 years ago
supertest-server v
Supertest Server
Starts up an HTTP server so you can run supertest API tests
Example
This is designed to be used in any test framework - the examples use Mocha, but should be agnostic enough to work with any framework.
var expect = require("chai").expect;
var supertest = require("supertest");
var Server = require("supertest-server");
var api = supertest("http://localhost:3000");
describe("Some test", function() {
var server;
before(function(done) {
server = new Server("node", [
"path/to/file.js",
"port=3000"
]).on("error", function(err) {
done(err);
}).on("data", function(data) {
if(data.match("--- SERVER UP ---")) {
/* Once we receive this, the test server is up */
done();
}
});
});
after(function() {
server.kill();
});
it("should connect", function(done) {
api
.get("/test")
.expect(200)
.expect("hello world")
.end(done);
});
});A word on starting a server
You will need to set up your server so that, when it starts up, it outputs something to the command line that indicates it has started successfully. In this example, it's listening for --- SERVER UP ---. Doing this is important as your server may output config or other information before the server is actually callable.
If you do not want to change how your server boots up in production, you can always add additional arguments at the point of boot.
0.1.0
12 years ago