add unit tests
This commit is contained in:
parent
01fbdbbfe8
commit
f2fe3a301b
4 changed files with 71 additions and 16 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
package.json
10
package.json
|
@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
32
server.js
32
server.js
|
@ -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,17 +24,20 @@ 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) {
|
||||||
transports: [
|
console.log('Enabling Logging...');
|
||||||
new winston.transports.Console({
|
app.use(expressWinston.logger({
|
||||||
colorize: true
|
transports: [
|
||||||
})
|
new winston.transports.Console({
|
||||||
],
|
colorize: true
|
||||||
meta: false,
|
})
|
||||||
msg: LOGGER_MESSAGE,
|
],
|
||||||
colorize: true,
|
meta: false,
|
||||||
statusLevels: true
|
msg: LOGGER_MESSAGE,
|
||||||
}));
|
colorize: 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 () {
|
||||||
console.log('Server started on port ' + server.address().port);
|
if (IN_TEST) {
|
||||||
|
console.log('Server started on port ' + server.address().port);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
module.exports = server;
|
||||||
|
|
42
tests/server.test.js
Normal file
42
tests/server.test.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Reference in a new issue