diff --git a/package.json b/package.json index 58051f1..bbc1c2f 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "postinstall": "typings install", "build": "tsc", "test": "npm run build && nsp check", + "mocha": "NODE_ENV=test mocha --compilers ts:ts-node/register --require scripts/test-helper.js src/__tests__/**.test.ts", "lint": "tslint src/**/*.ts" }, "engines": { @@ -41,9 +42,12 @@ "winston": "=2.3.1" }, "devDependencies": { + "chai": "=3.5.0", "mocha": "=3.2.0", + "node-fetch": "=1.6.3", "nsp": "=2.6.2", "supertest": "=3.0.0", + "ts-node": "=2.1.0", "tslint": "=4.4.2", "typescript": "=2.1.6", "typings": "=2.1.0" diff --git a/scripts/test-helper.js b/scripts/test-helper.js new file mode 100644 index 0000000..3856bdb --- /dev/null +++ b/scripts/test-helper.js @@ -0,0 +1,3 @@ +const chai = require('chai'); + +chai.expect(); diff --git a/src/__tests__/helpers.ts b/src/__tests__/helpers.ts new file mode 100644 index 0000000..7576f83 --- /dev/null +++ b/src/__tests__/helpers.ts @@ -0,0 +1,11 @@ +import createServer from '../server'; +import { Options } from '../types'; +import fetch from 'node-fetch'; + + + +export function runServer(opts: Object, url : string, callback: Function) { + return createServer(opts as Options).listen(1234, function () { + return fetch('http://0.0.0.0:1234' + url).then(callback); + }); +} diff --git a/src/__tests__/server.test.ts b/src/__tests__/server.test.ts new file mode 100644 index 0000000..517b85f --- /dev/null +++ b/src/__tests__/server.test.ts @@ -0,0 +1,24 @@ +import { expect } from 'chai'; +import { runServer } from './helpers'; + + +describe('Server', function () { + it('should test', function () { + expect(2 + 2).to.equal(4); + }); + + it('Should be usable', function (done) { + runServer({ + allowed_ips: [], + basicAuth: [], + dirList: false, + serveDir: 'site/', + opbeat: false, + open: false + }, '/index.html', function (response : any) { + expect(response.status).to.equal(200); + expect(response.url).to.include('/index.html'); + done(); + }); + }); +}); diff --git a/src/server.ts b/src/server.ts index 624d84c..cbb6801 100644 --- a/src/server.ts +++ b/src/server.ts @@ -15,7 +15,9 @@ import { Options } from './types'; export default function createServer(opts : Options) : express.Application { const app = express(); - app.use(logging); + if (process.env.NODE_ENV !== 'test') { + app.use(logging); + } if (opts.allowed_ips.length) { app.set('trust proxy', true); diff --git a/src/types/fakes.d.ts b/src/types/fakes.d.ts index 12ce20f..810a1dd 100644 --- a/src/types/fakes.d.ts +++ b/src/types/fakes.d.ts @@ -8,3 +8,4 @@ declare module 'express-winston'; declare module 'opbeat'; declare module 'docopt'; declare module 'open'; +declare module 'node-fetch'; diff --git a/tsconfig.json b/tsconfig.json index 13a0150..a2a5e35 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,8 @@ ], "exclude": [ "dist", - "node_modules" + "node_modules", + "scripts/" ], "typeRoots": [ "node_modules", diff --git a/typings.json b/typings.json index 86fe7f9..cea7d4a 100644 --- a/typings.json +++ b/typings.json @@ -8,5 +8,9 @@ }, "globalDependencies": { "node": "registry:dt/node#7.0.0+20170204020307" + }, + "globalDevDependencies": { + "chai": "registry:dt/chai#3.4.0+20170217154556", + "mocha": "registry:dt/mocha#2.2.5+20170204022515" } }