diff --git a/gulpfile.js b/gulpfile.js index 2589f70..1253314 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -6,7 +6,7 @@ const nodemon = require('gulp-nodemon') gulp.task('devel', () => { nodemon({ - script: 'src/app.js', + script: 'src/server.js', presets: ['env', 'stage-2'], exec: 'babel-node' }) diff --git a/package.json b/package.json index 87be7d7..7f70dfd 100644 --- a/package.json +++ b/package.json @@ -17,21 +17,57 @@ "@babel/node": "^7.0.0", "@babel/plugin-transform-async-to-generator": "^7.1.0", "@babel/preset-env": "^7.1.0", + "babel-core": "^7.0.0-bridge.0", "gulp": "^3.9.1", "gulp-babel": "^8.0.0-beta.2", "gulp-clean": "^0.4.0", "gulp-nodemon": "^2.2.1", "gulp-print": "^5.0.0", - "nodemon": "^1.18.4" + "jest": "^23.6.0", + "nodemon": "^1.18.4", + "supertest": "^3.3.0" }, "scripts": { - "start": "node app.js", + "start": "node server.js", + "test": "jest", + "testw": "jest --watchAll", "devel": "gulp devel", "clean": "gulp clean", "build": "gulp build", "image": "docker build -t statusteam/clicks-counter .", "push": "docker push statusteam/clicks-counter" }, + "jest": { + "moduleFileExtensions": [ + "js", + "json" + ], + "collectCoverage": true, + "collectCoverageFrom": [ + "src/**/*.js", + "!**/node_modules/**", + "!**/build/**", + "!**/coverage/**" + ], + "coverageThreshold": { + "global": { + "branches": 100, + "functions": 100, + "lines": 100, + "statements": 100 + } + }, + "coverageReporters": [ + "text", + "text-summary" + ], + "testRegex": "/test/.*.js$", + "testPathIgnorePatterns": [ + "/node_modules/", + "/build/", + "/coverage/" + ] + }, "keywords": [], "author": "", "license": "ISC" diff --git a/src/app.js b/src/app.js index 96d4348..ea0b1fb 100644 --- a/src/app.js +++ b/src/app.js @@ -9,7 +9,6 @@ import Redis from 'async-redis' /* DEFAULTS */ const REDIS_HOST = process.env.REDIS_HOST || 'localhost' const REDIS_PORT = process.env.REDIS_PORT || 6379 -const LISTEN_PORT = process.env.LISTEN_PORT || 3000 const app = new Koa() const router = new Router() @@ -23,6 +22,7 @@ app.on('error', (err, ctx) => { router.put('/clicks/:id', async ctx => { counter.incr(ctx.params.id) ctx.body = { [ctx.params.id]: await counter.state(ctx.params.id) } + ctx.status = 201 }); router.get('/clicks', async ctx => { @@ -38,6 +38,4 @@ app.use(Logger()) .use(router.routes()) .use(router.allowedMethods()) -app.listen(LISTEN_PORT) -console.log(`Redis connection: ${REDIS_HOST}:${REDIS_PORT}`) -console.log(`Started at: http://localhost:${LISTEN_PORT}/`) +module.exports = app diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..fa8240f --- /dev/null +++ b/src/server.js @@ -0,0 +1,10 @@ +import app from './app' + +/* DEFAULTS */ +const REDIS_HOST = process.env.REDIS_HOST || 'localhost' +const REDIS_PORT = process.env.REDIS_PORT || 6379 +const LISTEN_PORT = process.env.LISTEN_PORT || 3000 + +app.listen(LISTEN_PORT) +console.log(`Redis connection: ${REDIS_HOST}:${REDIS_PORT}`) +console.log(`Started at: http://localhost:${LISTEN_PORT}/`) diff --git a/test/app.js b/test/app.js new file mode 100644 index 0000000..10c2e16 --- /dev/null +++ b/test/app.js @@ -0,0 +1,12 @@ +import request from 'supertest' +import app from '../src/app' + +test('incrementing clicks works', async () => { + let resp = await request(app.callback()).put('/clicks/id') + expect(resp.status).toBe(201) +}) + +test('clicks works', async () => { + let resp = await request(app.callback()).get('/clicks') + expect(resp.status).toBe(200) +})