Created
January 22, 2017 20:52
-
-
Save crizstian/4e74645e070562ef9da8611a190703fe to your computer and use it in GitHub Desktop.
Example of unit test with mocha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-env mocha */ | |
const request = require('supertest') | |
const server = require('../server/server') | |
describe('Movies API', () => { | |
let app = null | |
let testMovies = [{ | |
'id': '3', | |
'title': 'xXx: Reactivado', | |
'format': 'IMAX', | |
'releaseYear': 2017, | |
'releaseMonth': 1, | |
'releaseDay': 20 | |
}, { | |
'id': '4', | |
'title': 'Resident Evil: Capitulo Final', | |
'format': 'IMAX', | |
'releaseYear': 2017, | |
'releaseMonth': 1, | |
'releaseDay': 27 | |
}, { | |
'id': '1', | |
'title': 'Assasins Creed', | |
'format': 'IMAX', | |
'releaseYear': 2017, | |
'releaseMonth': 1, | |
'releaseDay': 6 | |
}] | |
let testRepo = { | |
getAllMovies () { | |
return Promise.resolve(testMovies) | |
}, | |
getMoviePremiers () { | |
return Promise.resolve(testMovies.filter(movie => movie.releaseYear === 2017)) | |
}, | |
getMovieById (id) { | |
return Promise.resolve(testMovies.find(movie => movie.id === id)) | |
} | |
} | |
beforeEach(() => { | |
return server.start({ | |
port: 3000, | |
repo: testRepo | |
}).then(serv => { | |
app = serv | |
}) | |
}) | |
afterEach(() => { | |
app.close() | |
app = null | |
}) | |
it('can return all movies', (done) => { | |
request(app) | |
.get('/movies') | |
.expect((res) => { | |
res.body.should.containEql({ | |
'id': '1', | |
'title': 'Assasins Creed', | |
'format': 'IMAX', | |
'releaseYear': 2017, | |
'releaseMonth': 1, | |
'releaseDay': 6 | |
}) | |
}) | |
.expect(200, done) | |
}) | |
it('can get movie premiers', (done) => { | |
request(app) | |
.get('/movies/premiers') | |
.expect((res) => { | |
res.body.should.containEql({ | |
'id': '1', | |
'title': 'Assasins Creed', | |
'format': 'IMAX', | |
'releaseYear': 2017, | |
'releaseMonth': 1, | |
'releaseDay': 6 | |
}) | |
}) | |
.expect(200, done) | |
}) | |
it('returns 200 for an known movie', (done) => { | |
request(app) | |
.get('/movies/1') | |
.expect((res) => { | |
res.body.should.containEql({ | |
'id': '1', | |
'title': 'Assasins Creed', | |
'format': 'IMAX', | |
'releaseYear': 2017, | |
'releaseMonth': 1, | |
'releaseDay': 6 | |
}) | |
}) | |
.expect(200, done) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-env mocha */ | |
const server = require('./server') | |
describe('Server', () => { | |
it('should require a port to start', () => { | |
return server.start({ | |
repo: {} | |
}).should.be.rejectedWith(/port/) | |
}) | |
it('should require a repository to start', () => { | |
return server.start({ | |
port: {} | |
}).should.be.rejectedWith(/repository/) | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm able to run test after adding to the top
require('should')