Skip to content

Commit

Permalink
Add API integration testing
Browse files Browse the repository at this point in the history
Create new integration tests for notes API. In these tests we can
test the whole API, check HTTP response codes and content.
  • Loading branch information
CarlosRodrigo committed Jun 6, 2019
1 parent bc37f74 commit efc62e1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- run:
name: Run tests and upload to coveralls
command: |
echo $FIREBASE_DB_STAG | base64 -di > src/environment/FirestoreDB/keys/serviceAccountKey.json
npm run test-coverage
npm run test-coveralls
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ console.log(config)
console.log(server)

server.start()

// Export the app for testing
module.exports = app
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"start": "./node_modules/eslint/bin/eslint.js . && node index.js",
"lint": "./node_modules/eslint/bin/eslint.js .",
"test": "mocha --recursive --exit",
"test-coverage": "nyc --all true mocha --recursive --exit",
"test": "mocha --recursive --exit --timeout 10000",
"test-coverage": "nyc --all true npm run test",
"test-coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"repository": {
Expand All @@ -23,6 +23,7 @@
"devDependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-http": "^4.3.0",
"coveralls": "^3.0.3",
"eslint": "^5.3.0",
"eslint-config-standard": "^12.0.0-alpha.0",
Expand Down
77 changes: 77 additions & 0 deletions test/api/notes/notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Import the dependencies for testing
const chai = require('chai')
const chaiHttp = require('chai-http')
const mocha = require('mocha')
const app = require('../../../index')

const describe = mocha.describe
const it = mocha.it
const expect = chai.expect

// Configure chai
chai.use(chaiHttp)
chai.should()

var noteId
let note = {
'topic': 'test',
'description': 'API Testing',
'user': 'Tester',
'session_id': 'test'
}

describe('Notes API', function () {
describe('/POST notes', function () {
it('Should insert a note', function () {
return chai.request(app)
.post('/notes')
.send(note)
.then((res) => {
res.should.have.status(200)
res.body.should.be.a('object')

expect(res.body.topic).to.equal(note.topic)

noteId = res.body.id
})
})
})

describe('/GET notes from sessions', function () {
it('Should get notes from session', function () {
return chai.request(app)
.get('/notes/test')
.then((res) => {
res.should.have.status(200)
res.body.should.be.a('array')

expect(res.body[0].description).to.equal(note.description)
})
})
})

describe('/PUT notes', function () {
it('Should update a note', function () {
var newDescription = 'updated note'
note.description = newDescription
return chai.request(app)
.put('/notes/' + noteId)
.send(note)
.then((res) => {
res.should.have.status(200)
res.body.should.be.a('object')
})
})
})

describe('/DELETE note', function () {
it('Should delete note', function () {
return chai.request(app)
.delete('/notes/' + noteId)
.then((res) => {
res.should.have.status(200)
res.body.should.be.a('object')
})
})
})
})

0 comments on commit efc62e1

Please sign in to comment.