Skip to content

Commit 4babb07

Browse files
committed
Merge pull request #505 from kingcody/feature/mocha-istanbul
feat(server-tests): code coverage and e2e
2 parents c018ae8 + dbbaa20 commit 4babb07

19 files changed

+770
-88
lines changed

Diff for: app/templates/Gruntfile.js

+92-5
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ module.exports = function (grunt) {
169169
},
170170
src: [
171171
'server/**/*.js',
172-
'!server/**/*.spec.js'
172+
'!server/**/*.{spec,e2e}.js'
173173
]
174174
},
175175
serverTest: {
176176
options: {
177177
jshintrc: 'server/.jshintrc-spec'
178178
},
179-
src: ['server/**/*.spec.js']
179+
src: ['server/**/*.{spec,e2e}.js']
180180
},
181181
all: [
182182
'<%%= yeoman.client %>/{app,components}/**/*.js',
@@ -486,9 +486,60 @@ module.exports = function (grunt) {
486486

487487
mochaTest: {
488488
options: {
489-
reporter: 'spec'
489+
reporter: 'spec',
490+
require: 'mocha.conf.js'
490491
},
491-
src: ['server/**/*.spec.js']
492+
unit: {
493+
src: ['server/**/*.spec.js']
494+
},
495+
e2e: {
496+
src: ['server/**/*.e2e.js']
497+
}
498+
},
499+
500+
mocha_istanbul: {
501+
unit: {
502+
options: {
503+
excludes: [
504+
'**/*.spec.js',
505+
'**/*.mock.js',
506+
'**/*.e2e.js'
507+
],
508+
reporter: 'spec',
509+
require: ['mocha.conf.js'],
510+
mask: '**/*.spec.js',
511+
coverageFolder: 'coverage/unit'
512+
},
513+
src: 'server'
514+
},
515+
e2e: {
516+
options: {
517+
excludes: [
518+
'**/*.spec.js',
519+
'**/*.mock.js',
520+
'**/*.e2e.js'
521+
],
522+
reporter: 'spec',
523+
require: ['mocha.conf.js'],
524+
mask: '**/*.e2e.js',
525+
coverageFolder: 'coverage/e2e'
526+
},
527+
src: 'server'
528+
}
529+
},
530+
531+
istanbul_check_coverage: {
532+
default: {
533+
options: {
534+
coverageFolder: 'coverage/*',
535+
check: {
536+
lines: 80,
537+
statements: 80,
538+
branches: 80,
539+
functions: 80
540+
}
541+
}
542+
}
492543
},
493544

494545
protractor: {
@@ -769,7 +820,8 @@ module.exports = function (grunt) {
769820
return grunt.task.run([
770821
'env:all',
771822
'env:test',
772-
'mochaTest'
823+
'mochaTest:unit',
824+
'mochaTest:e2e'
773825
]);
774826
}
775827

@@ -804,6 +856,41 @@ module.exports = function (grunt) {
804856
]);
805857
}
806858

859+
else if (target === 'coverage') {
860+
861+
if (option === 'unit') {
862+
return grunt.task.run([
863+
'env:all',
864+
'env:test',
865+
'mocha_istanbul:unit'
866+
]);
867+
}
868+
869+
else if (option === 'e2e') {
870+
return grunt.task.run([
871+
'env:all',
872+
'env:test',
873+
'mocha_istanbul:e2e'
874+
]);
875+
}
876+
877+
else if (option === 'check') {
878+
return grunt.task.run([
879+
'istanbul_check_coverage'
880+
]);
881+
}
882+
883+
else {
884+
return grunt.task.run([
885+
'env:all',
886+
'env:test',
887+
'mocha_istanbul',
888+
'istanbul_check_coverage'
889+
]);
890+
}
891+
892+
}
893+
807894
else grunt.task.run([
808895
'test:server',
809896
'test:client'

Diff for: app/templates/_package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"socketio-jwt": "^2.0.2"<% } %>
3131
},
3232
"devDependencies": {
33+
"chai-as-promised": "^4.1.1",
3334
"grunt": "~0.4.4",
3435
"grunt-autoprefixer": "~0.7.2",
3536
"grunt-wiredep": "~1.8.0",
@@ -61,7 +62,8 @@
6162
"grunt-asset-injector": "^0.1.0",
6263
"grunt-karma": "~0.8.2",
6364
"grunt-build-control": "DaftMonk/grunt-build-control",
64-
"grunt-mocha-test": "~0.10.2",<% if(filters.sass) { %>
65+
"grunt-mocha-test": "~0.10.2",
66+
"grunt-mocha-istanbul": "^2.0.0",<% if(filters.sass) { %>
6567
"grunt-contrib-sass": "^0.7.3",<% } %><% if(filters.stylus) { %>
6668
"grunt-contrib-stylus": "latest",<% } %>
6769
"jit-grunt": "^0.5.0",
@@ -85,8 +87,9 @@
8587
"karma-phantomjs-launcher": "~0.1.4",
8688
"karma": "~0.12.9",
8789
"karma-ng-html2js-preprocessor": "~0.1.0",
90+
"proxyquire": "^1.0.1",
8891
"supertest": "~0.11.0",
89-
"should": "~3.3.1"
92+
"sinon-chai": "^2.5.0"
9093
},
9194
"engines": {
9295
"node": ">=0.10.0"

Diff for: app/templates/mocha.conf.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var chai = require('chai');
4+
var sinon = require('sinon');
5+
var sinonChai = require('sinon-chai');
6+
var chaiAsPromised = require('chai-as-promised');
7+
8+
global.expect = chai.expect;
9+
global.assert = chai.assert;
10+
global.sinon = sinon;
11+
12+
chai.should();
13+
chai.use(sinonChai);
14+
chai.use(chaiAsPromised);

Diff for: app/templates/server/.jshintrc-spec

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"before": true,
77
"beforeEach": true,
88
"after": true,
9-
"afterEach": true
9+
"afterEach": true,
10+
"expect": true,
11+
"assert": true,
12+
"sinon": true
1013
}
1114
}

Diff for: app/templates/server/api/thing/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ router.put('/:id', controller.update);
1212
router.patch('/:id', controller.update);
1313
router.delete('/:id', controller.destroy);<% } %>
1414

15-
module.exports = router;
15+
module.exports = router;

Diff for: app/templates/server/api/thing/index.spec.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
var proxyquire = require('proxyquire').noPreserveCache();
4+
5+
/* thing.controller stub */
6+
var thingCtrl = {
7+
index: 'thingCtrl.index'<% if(filters.mongoose) { %>,
8+
show: 'thingCtrl.show',
9+
create: 'thingCtrl.create',
10+
update: 'thingCtrl.update',
11+
destroy: 'thingCtrl.destroy'<% } %>
12+
},
13+
/* express.Router().router stub */
14+
router = {
15+
get: sinon.spy()<% if(filters.mongoose) { %>,
16+
put: sinon.spy(),
17+
patch: sinon.spy(),
18+
post: sinon.spy(),
19+
delete: sinon.spy()<% } %>
20+
},
21+
/* stubbed thing router */
22+
index = proxyquire('./index.js', {
23+
'express': {
24+
Router: function() {
25+
return router;
26+
}
27+
},
28+
'./thing.controller': thingCtrl
29+
});
30+
31+
describe('Thing API Router:', function() {
32+
33+
it('should return an express router instance', function() {
34+
index.should.equal(router);
35+
});
36+
37+
describe('GET /api/things', function() {
38+
39+
it('should route to thing.controller.index', function() {
40+
return router.get.withArgs('/', 'thingCtrl.index').should.have.been.calledOnce;
41+
});
42+
43+
});<% if(filters.mongoose) { %>
44+
45+
describe('GET /api/things/:id', function() {
46+
47+
it('should route to thing.controller.show', function() {
48+
return router.get.withArgs('/:id', 'thingCtrl.show').should.have.been.calledOnce;
49+
});
50+
51+
});
52+
53+
describe('POST /api/things', function() {
54+
55+
it('should route to thing.controller.create', function() {
56+
return router.post.withArgs('/', 'thingCtrl.create').should.have.been.calledOnce;
57+
});
58+
59+
});
60+
61+
describe('PUT /api/things/:id', function() {
62+
63+
it('should route to thing.controller.update', function() {
64+
return router.put.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
65+
});
66+
67+
});
68+
69+
describe('PATCH /api/things/:id', function() {
70+
71+
it('should route to thing.controller.update', function() {
72+
return router.patch.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
73+
});
74+
75+
});
76+
77+
describe('DELETE /api/things/:id', function() {
78+
79+
it('should route to thing.controller.destroy', function() {
80+
return router.delete.withArgs('/:id', 'thingCtrl.destroy').should.have.been.calledOnce;
81+
});
82+
83+
});<% } %>
84+
85+
});

Diff for: app/templates/server/api/thing/thing.controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@ exports.destroy = function(req, res) {
8686

8787
function handleError(res, err) {
8888
return res.send(500, err);
89-
}<% } %>
89+
}<% } %>

0 commit comments

Comments
 (0)