Skip to content

Commit

Permalink
[Assignment 3] Testing + Code Coverage
Browse files Browse the repository at this point in the history
Closes outmoded#79

This introduces basic testing and code coverage at 94.74%  There is definitely still some weirdness with testing error handling when Hoek is asserting.  I'm still not sure the best way to teardown the server if no callback is given.
  • Loading branch information
Myles Borins committed Mar 25, 2015
1 parent 6f84807 commit bd9b142
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
7 changes: 6 additions & 1 deletion package.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
"hoek": "2.x.x"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "lab -La code -t 100",
"test-cov-html": "lab -La code -r html -o coverage.html",
"start": "node lib/index.js"
},
"devDependencies": {
"code": "^1.3.0",
"lab": "^5.5.1"
}
}
52 changes: 52 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Load modules

var Code = require('code');
var Lab = require('lab');
var lib = require('../lib');


// Declare internals

var internals = {};


// Test shortcuts

var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;

describe('lib', function() {

describe('.init()', function () {

it('should throw when given an invalid port', function (done) {

expect(function () {

lib.init('I am going to throw');
}).to.throw('Invalid connection options {\n \"router\": {\n \"isCaseSensitive\": true,\n \"stripTrailingSlash\": false\n },\n \"routes\": {\n \"cache\": {\n \"statuses\": [\n 200\n ]\n },\n \"cors\": null,\n \"files\": {\n \"relativeTo\": \".\"\n },\n \"json\": {\n \"replacer\": null,\n \"space\": null,\n \"suffix\": null\n },\n \"payload\": {\n \"failAction\": \"error\",\n \"maxBytes\": 1048576,\n \"output\": \"data\",\n \"parse\": true,\n \"timeout\": 10000,\n \"uploads\": \"/var/folders/79/1_fjgmlj047ft1bc5s1hd_780000gn/T/\"\n },\n \"response\": {\n \"options\": {}\n },\n \"security\": null,\n \"state\": {\n \"parse\": true,\n \"failAction\": \"error\"\n },\n \"timeout\": {\n \"server\": false\n },\n \"validate\": {\n \"options\": {}\n }\n },\n \"port\" \u001b[31m[1, 2, 3]\u001b[0m: \"I am going to throw\"\n}\n\u001b[31m\n[1] \"port\" must be a number\n[2] \"port\" with value \"I am going to throw\" fails to match the required pattern: /\\//\n[3] \"port\" with value \"I am going to throw\" fails to match the required pattern: /^\\\\\\\\\\.\\\\pipe\\\\/\u001b[0m');
done();
});

it('should return an instance of server', function (done) {

var server = lib.init();
setTimeout(function () {
expect(server).to.not.be.undefined();
server.stop(done);
}, 275);
});

it('should create a server on port 8000 when no port is given', function (done) {

lib.init(function (err, server) {

expect(err).to.be.undefined();
expect(server.info.port).to.equal(8000);
server.stop(done);
});
});
});
});
38 changes: 38 additions & 0 deletions test/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Load modules

var Code = require('code');
var Lab = require('lab');
var lib = require('../lib');
var Pkg = require('../package.json');


// Declare internals

var internals = {};


// Test shortcuts

var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;


describe('version', function() {

it('should return expected data', function (done) {

lib.init(function (err, server) {

expect(err).to.be.undefined();
expect(server.info.port).to.equal(8000);
server.inject('/version', function (response) {

expect(response.statusCode).to.equal(200);
expect(response.result.version).to.equal(Pkg.version);
server.stop(done);
});
});
});
});

0 comments on commit bd9b142

Please sign in to comment.