add unit tests

This commit is contained in:
Jake Howard 2016-10-30 22:21:10 +00:00
parent 01fbdbbfe8
commit f2fe3a301b
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 71 additions and 16 deletions

View file

@ -2,6 +2,7 @@
"extends": "./node_modules/eslint-config/.eslintrc", "extends": "./node_modules/eslint-config/.eslintrc",
"env": { "env": {
"node": true, "node": true,
"browser": false "browser": false,
"mocha": true
} }
} }

View file

@ -4,8 +4,10 @@
"description": "Container to host simple static applications using a node server, so files can be deployed using rsync", "description": "Container to host simple static applications using a node server, so files can be deployed using rsync",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"test": "eslint server.js && nsp check", "test": "npm run mocha && npm run lint && nsp check",
"start": "node server.js" "lint": "eslint server.js tests/**.js",
"start": "node server.js",
"mocha": "NODE_ENV=test mocha tests/**.test.js"
}, },
"engines": { "engines": {
"node": "5.11.1" "node": "5.11.1"
@ -30,6 +32,8 @@
"devDependencies": { "devDependencies": {
"eslint": "=1.9.0", "eslint": "=1.9.0",
"eslint-config": "git://github.com/dabapps/eslint-config.git", "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"
} }
} }

View file

@ -6,6 +6,7 @@ const path = require('path');
const winston = require('winston'); const winston = require('winston');
const expressWinston = require('express-winston'); const expressWinston = require('express-winston');
const IN_TEST = process.env.NODE_ENV === 'true';
const PORT = process.env.PORT || 5000; const PORT = process.env.PORT || 5000;
const SERVE_DIR = path.join(__dirname, '/site'); const SERVE_DIR = path.join(__dirname, '/site');
const PAGE_404 = path.join(SERVE_DIR, '.404.html'); const PAGE_404 = path.join(SERVE_DIR, '.404.html');
@ -23,7 +24,9 @@ const app = express();
app.use(compression({ level: 9 })); app.use(compression({ level: 9 }));
app.use(helmet()); app.use(helmet());
app.use(expressWinston.logger({ if (IN_TEST) {
console.log('Enabling Logging...');
app.use(expressWinston.logger({
transports: [ transports: [
new winston.transports.Console({ new winston.transports.Console({
colorize: true colorize: true
@ -33,7 +36,8 @@ app.use(expressWinston.logger({
msg: LOGGER_MESSAGE, msg: LOGGER_MESSAGE,
colorize: true, colorize: true,
statusLevels: true statusLevels: true
})); }));
}
app.use(function (request, response, next) { app.use(function (request, response, next) {
if (request.url.endsWith('/')) { if (request.url.endsWith('/')) {
@ -51,5 +55,9 @@ app.use(function (request, response, next) {
const server = app.listen(PORT, function () { const server = app.listen(PORT, function () {
if (IN_TEST) {
console.log('Server started on port ' + server.address().port); console.log('Server started on port ' + server.address().port);
}
}); });
module.exports = server;

42
tests/server.test.js Normal file
View file

@ -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);
});
});
});