From f2fe3a301b5596c3fc204459113c619e85df93d4 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 30 Oct 2016 22:21:10 +0000 Subject: [PATCH] add unit tests --- .eslintrc | 3 ++- package.json | 10 +++++++--- server.js | 32 ++++++++++++++++++++------------ tests/server.test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 tests/server.test.js diff --git a/.eslintrc b/.eslintrc index 14d2fee..22121b5 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,6 +2,7 @@ "extends": "./node_modules/eslint-config/.eslintrc", "env": { "node": true, - "browser": false + "browser": false, + "mocha": true } } diff --git a/package.json b/package.json index 82fd77f..fc78af2 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,10 @@ "description": "Container to host simple static applications using a node server, so files can be deployed using rsync", "main": "server.js", "scripts": { - "test": "eslint server.js && nsp check", - "start": "node server.js" + "test": "npm run mocha && npm run lint && nsp check", + "lint": "eslint server.js tests/**.js", + "start": "node server.js", + "mocha": "NODE_ENV=test mocha tests/**.test.js" }, "engines": { "node": "5.11.1" @@ -30,6 +32,8 @@ "devDependencies": { "eslint": "=1.9.0", "eslint-config": "git://github.com/dabapps/eslint-config.git", - "nsp": "=2.6.1" + "mocha": "=3.1.2", + "nsp": "=2.6.1", + "supertest": "=2.0.1" } } diff --git a/server.js b/server.js index a33bb38..a3fefa8 100644 --- a/server.js +++ b/server.js @@ -6,6 +6,7 @@ const path = require('path'); const winston = require('winston'); const expressWinston = require('express-winston'); +const IN_TEST = process.env.NODE_ENV === 'true'; const PORT = process.env.PORT || 5000; const SERVE_DIR = path.join(__dirname, '/site'); const PAGE_404 = path.join(SERVE_DIR, '.404.html'); @@ -23,17 +24,20 @@ const app = express(); app.use(compression({ level: 9 })); app.use(helmet()); -app.use(expressWinston.logger({ - transports: [ - new winston.transports.Console({ - colorize: true - }) - ], - meta: false, - msg: LOGGER_MESSAGE, - colorize: true, - statusLevels: true -})); +if (IN_TEST) { + console.log('Enabling Logging...'); + app.use(expressWinston.logger({ + transports: [ + new winston.transports.Console({ + colorize: true + }) + ], + meta: false, + msg: LOGGER_MESSAGE, + colorize: true, + statusLevels: true + })); +} app.use(function (request, response, next) { if (request.url.endsWith('/')) { @@ -51,5 +55,9 @@ app.use(function (request, response, next) { const server = app.listen(PORT, function () { - console.log('Server started on port ' + server.address().port); + if (IN_TEST) { + console.log('Server started on port ' + server.address().port); + } }); + +module.exports = server; diff --git a/tests/server.test.js b/tests/server.test.js new file mode 100644 index 0000000..0caef19 --- /dev/null +++ b/tests/server.test.js @@ -0,0 +1,42 @@ +const request = require('supertest'); +const fs = require('fs'); + + +describe('Server', function () { + var server; + before(function () { + server = require('../server'); + }); + + after(function () { + server.close(); + }); + + it('responds to /', function (done) { + request(server) + .get('/') + .expect(200, done); + }); + + it('returns 404 on bad path', function (done) { + request(server) + .get('/foo/bar') + .expect(404, done); + }); + + describe('index route', function () { + const body = fs.readFileSync(__dirname + '/../site/index.html').toString(); + + it('should render /index.html', function (done) { + request(server) + .get('/index.html') + .expect(200, body, done); + }); + + it('should render /', function (done) { + request(server) + .get('/') + .expect(200, body, done); + }); + }); +});