Skip to content

Commit e773a59

Browse files
committed
Basic unit tests
1 parent eb1daa3 commit e773a59

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

buildspecs/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ phases:
44
install:
55
commands:
66
- npm install
7-
7+
build:
8+
commands:
9+
- npm test
810
artifacts:
911
files:
1012
- '**/*'

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
"version": "1.0.0",
44
"main": "service.js",
55
"scripts": {
6-
"start": "node service.js"
6+
"start": "node service.js",
7+
"test": "./node_modules/mocha/bin/mocha"
78
},
89
"dependencies": {
910
"numeral": "^2.0.0",
1011
"express": "^4.13.3"
1112
},
13+
"devDependencies": {
14+
"mocha": "^4.0.0",
15+
"chai": "^3.5.0",
16+
"chai-http": "^3.0.0"
17+
},
1218
"private": "true"
1319
}

service.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ app.get('/divide', function (req, res) {
5353
sendResult(value, res);
5454
})
5555

56-
app.listen(PORT, HOST);
57-
console.log(`Running on http://${HOST}:${PORT}`);
56+
if (!module.parent) {
57+
app.listen(PORT, HOST);
58+
}
59+
module.exports = app;

test/calculator_spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var assert = require('assert');
2+
3+
var calc = require('../calculator.js');
4+
5+
describe('Calculator Tests', function() {
6+
describe('Addition Tests', function() {
7+
it('returns 1 + 1 = 2', function(done) {
8+
assert.equal(calc.add(1, 1), 2);
9+
done();
10+
});
11+
12+
it('returns 1 + -1 = 0', function(done) {
13+
assert.equal(calc.add(1, -1), 0);
14+
done();
15+
});
16+
});
17+
18+
describe('Multiplication Tests', function() {
19+
it('returns 2 * 2 = 4', function(done) {
20+
assert.equal(calc.multiply(2, 2), 4);
21+
done();
22+
});
23+
24+
it('returns 0 * 4 = 4', function(done) {
25+
assert.equal(calc.multiply(2, 2), 4);
26+
done();
27+
});
28+
});
29+
});

test/service_spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
process.env.NODE_ENV = 'test';
2+
3+
const chai = require('chai');
4+
const chaiHttp = require('chai-http');
5+
const should = chai.should();
6+
const expect = chai.expect;
7+
const app = require('../service');
8+
9+
chai.use(chaiHttp);
10+
11+
describe('Calculator service', () => {
12+
13+
var server;
14+
15+
it('it should add two numbers', (done) => {
16+
server = app.listen();
17+
chai.request(server)
18+
.get('/add')
19+
.query({a: 1, b: 2})
20+
.end((err, res) => {
21+
expect(err).to.be.null;
22+
res.should.have.status(200);
23+
res.should.have.header('content-type', 'text/plain; charset=utf-8');
24+
res.text.should.equal('3');
25+
done();
26+
});
27+
});
28+
29+
after(function(done) {
30+
if (server) {
31+
server.close(done);
32+
}
33+
});
34+
});

0 commit comments

Comments
 (0)