Skip to content

Commit

Permalink
add some integration tests for the Cat API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Verhoelen committed May 1, 2019
1 parent a49292a commit 0bfdf5c
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 8 deletions.
163 changes: 163 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
},
"scripts": {
"start": "ts-node service/server/index.ts",
"prettier:check": "prettier --check service/**/*.{ts,tsx,js,jsx}",
"prettier:write": "prettier --write service/**/*.{ts,tsx,js,jsx}",
"prettier:check": "prettier --check service/**/*.{ts,tsx,jsx}",
"prettier:write": "prettier --write service/**/*.{ts,tsx,jsx}",
"test": "npm run prettier:check && npm run test:unit && npm audit",
"test:unit": "_mocha 'service/**/*Spec.ts' --opts service/mocha.opts"
"test:unit": "_mocha 'service/**/*Spec.ts' --opts service/mocha.opts",
"test:integration": "_mocha 'test/integration/**/*Test.ts' --opts service/mocha.opts"
},
"dependencies": {
"express": "^4.16.2",
"compression": "1.7.2",
"cookie-parser": "1.4.3",
"http-status-codes": "1.3.0",
"compression": "^1.7.2",
"cookie-parser": "^1.4.3",
"http-status-codes": "^1.3.0",
"connect-datadog": "^0.0.6",
"hot-shots": "^4.7.0"
"hot-shots": "^4.7.0",
"supertest": "^3.0.0",
"tslib": "^1.9.3"
},
"devDependencies": {
"@types/chai": "4.1.7",
Expand All @@ -30,6 +33,7 @@
"@types/compression": "0.0.35",
"@types/cookie-parser": "1.4.1",
"@types/express": "4.11.1",
"@types/supertest": "2.0.4",
"@types/mocha": "5.2.5",
"@types/sinon-chai": "3.2.2",
"chai": "4.2.0",
Expand Down
2 changes: 1 addition & 1 deletion service/server/ExpressServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ExpressServer {

private configureApiEndpoints(server: Express) {
server.get('/api/cat', noCache, this.catEndpoints.getAllCats)
server.get('/api/statistics/cat', noCache, this.catEndpoints.getCatStatistics)
server.get('/api/statistics/cat', noCache, this.catEndpoints.getCatsStatistics)
server.get('/api/cat/:catId', noCache, this.catEndpoints.getCatDetails)
}
}
71 changes: 71 additions & 0 deletions test/integration/CatsApiTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as request from 'supertest'
import { expect } from 'chai'
import * as HttpStatus from 'http-status-codes'
import TestEnv from './TestEnv'

const { baseUrl } = TestEnv

describe('Cats API', () => {
describe('Cat details', () => {
it('should respond with a positive status code if the cat is known', () => {
return request(baseUrl)
.get('/api/cat/1')
.expect(HttpStatus.OK)
})

it('should respond with 404 status code if the cat is not known', () => {
return request(baseUrl)
.get('/api/cat/666')
.expect(HttpStatus.NOT_FOUND)
})

it('should respond with cat details data if the cat is known', () => {
return request(baseUrl)
.get('/api/cat/1')
.expect((res: Response) => {
expect(res.body).to.deep.equal({
id: 1,
name: 'Tony Iommi',
breed: 'British Shorthair',
gender: 'male',
age: 71
})
})
})
})

describe('All cats', () => {
it('should respond with a positive status code', () => {
return request(baseUrl)
.get('/api/cat')
.expect(HttpStatus.OK)
})

it('should return a reasonable amount of cats entries', () => {
return request(baseUrl)
.get('/api/cat')
.expect((res: Response) => {
expect((res.body as any).length).to.eq(5)
})
})
})

describe('Cat statistics', () => {
it('should respond with a positive status code', () => {
return request(baseUrl)
.get('/api/statistics/cat')
.expect(HttpStatus.OK)
})

it('should deliver a reasonable statistics response', () => {
return request(baseUrl)
.get('/api/statistics/cat')
.expect((res: Response) => {
expect(res.body).to.deep.eq({
amount: 5,
averageAge: 69.2
})
})
})
})
})
4 changes: 4 additions & 0 deletions test/integration/TestEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default class TestEnv {
public static readonly local = !process.env.BASE_URL
public static readonly baseUrl = process.env.BASE_URL || 'http://localhost:8000'
}

0 comments on commit 0bfdf5c

Please sign in to comment.