add jest for running tests

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2018-11-14 17:18:44 +01:00
parent 792f61d55e
commit 1de51e3f2a
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
5 changed files with 63 additions and 7 deletions

View File

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

View File

@ -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"

View File

@ -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

10
src/server.js Normal file
View File

@ -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}/`)

12
test/app.js Normal file
View File

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