Skip to content

Commit dbbaa20

Browse files
committed
feat(server-tests): code coverage and e2e
Changes: - split server tests into `unit` and `e2e` type test - adjust `mochaTest` and `test:server` tasks to reflect the two types of tests - implement `grunt-mocha-istanbul` task for server-side code coverage - add `test:coverage` task - improve `mocha.conf.js` - add sinon, expect, and assert as globals to `server/.jshintrc-spec` - add `proxyquire` to the app for better stubbing in unit tests - improve test to reflect recent changes and to lay ground work for more coverage Grunt Task `test`: The grunt 'test' task now has a 'coverage' target. Used like so `grunt test:coverage`. This task will run istanbul for the server unit tests and e2e test separately. The resulting coverage reports will be placed in `coverage/(unit|e2e)`. There is also an option for the `test:coverage` task, possibilities for option are: - `unit` - `e2e` - `check` `test:coverage:check` will check the coverage reports for both `unit` and `e2e` and report whether or not they meet the required coverage.
1 parent de5c36c commit dbbaa20

19 files changed

+748
-75
lines changed

Diff for: app/templates/Gruntfile.js

+90-4
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',
@@ -489,7 +489,57 @@ module.exports = function (grunt) {
489489
reporter: 'spec',
490490
require: 'mocha.conf.js'
491491
},
492-
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+
}
493543
},
494544

495545
protractor: {
@@ -770,7 +820,8 @@ module.exports = function (grunt) {
770820
return grunt.task.run([
771821
'env:all',
772822
'env:test',
773-
'mochaTest'
823+
'mochaTest:unit',
824+
'mochaTest:e2e'
774825
]);
775826
}
776827

@@ -805,6 +856,41 @@ module.exports = function (grunt) {
805856
]);
806857
}
807858

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+
808894
else grunt.task.run([
809895
'test:server',
810896
'test:client'

Diff for: app/templates/_package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"grunt-asset-injector": "^0.1.0",
6363
"grunt-karma": "~0.8.2",
6464
"grunt-build-control": "DaftMonk/grunt-build-control",
65-
"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) { %>
6667
"grunt-contrib-sass": "^0.7.3",<% } %><% if(filters.stylus) { %>
6768
"grunt-contrib-stylus": "latest",<% } %>
6869
"jit-grunt": "^0.5.0",
@@ -86,6 +87,7 @@
8687
"karma-phantomjs-launcher": "~0.1.4",
8788
"karma": "~0.12.9",
8889
"karma-ng-html2js-preprocessor": "~0.1.0",
90+
"proxyquire": "^1.0.1",
8991
"supertest": "~0.11.0",
9092
"sinon-chai": "^2.5.0"
9193
},

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

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var chaiAsPromised = require('chai-as-promised');
77

88
global.expect = chai.expect;
99
global.assert = chai.assert;
10+
global.sinon = sinon;
11+
1012
chai.should();
1113
chai.use(sinonChai);
1214
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)