Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
feat(tests): add client and server test coverage to Gulp (#1466)
Browse files Browse the repository at this point in the history
* Added coverage to Gulp

* Integrated coveralls tests with gulp

* spurious changes to kick off coveralls
  • Loading branch information
hyperreality authored and lirantal committed Sep 3, 2016
1 parent c2cfc38 commit 7be1910
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ app/tests/coverage/
.bower-*/
.idea/
coverage/
.coveralls.yml

# MEAN.js app and assets
# ======================
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ before_install:
- sleep 3
after_script:
- nsp audit-package
- grunt coverage
- gulp test:coverage
notifications:
webhooks:
urls:
Expand Down
2 changes: 1 addition & 1 deletion config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ module.exports = {
}
}
},
// This config is set to true during grunt coverage
// This config is set to true during gulp coverage
coverage: process.env.COVERAGE || false
};
49 changes: 48 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var _ = require('lodash'),
fs = require('fs'),
defaultAssets = require('./config/assets/default'),
testAssets = require('./config/assets/test'),
testConfig = require('./config/env/test'),
glob = require('glob'),
gulp = require('gulp'),
gulpLoadPlugins = require('gulp-load-plugins'),
Expand All @@ -23,7 +24,8 @@ var _ = require('lodash'),
protractor = require('gulp-protractor').protractor,
webdriver_update = require('gulp-protractor').webdriver_update,
webdriver_standalone = require('gulp-protractor').webdriver_standalone,
KarmaServer = require('karma').Server;
KarmaServer = require('karma').Server,
lcovMerger = require('lcov-result-merger');

// Local settings
var changedTestFiles = [];
Expand Down Expand Up @@ -318,7 +320,48 @@ gulp.task('mocha', function (done) {
});
});
});
});

// Add configuration options for coverage here
gulp.task('configure-coverage', function (done) {
// Set coverage config environment variable so karma-coverage knows to run it
testConfig.coverage = true;
done();
});

// Prepare istanbul coverage test
gulp.task('pre-test', function () {

// Display coverage for all server JavaScript files
return gulp.src(defaultAssets.server.allJS)
// Covering files
.pipe(plugins.istanbul())
// Force `require` to return covered files
.pipe(plugins.istanbul.hookRequire());
});

// Run istanbul test and write report
gulp.task('mocha:coverage', ['pre-test', 'mocha'], function () {
var testSuites = changedTestFiles.length ? changedTestFiles : testAssets.tests.server;

return gulp.src(testSuites)
.pipe(plugins.istanbul.writeReports({
reportOpts: { dir: './coverage/server' }
}));
});

// Join the coverage files for client and server into a single file
// Otherwise they get sent to coveralls as separate builds
gulp.task('merge-lcov', function (done) {
return gulp.src('./coverage/**/lcov.info')
.pipe(lcovMerger())
.pipe(gulp.dest('./coverage/merged/'));
});

// Send coverage test results to coveralls
gulp.task('coveralls', ['merge-lcov'], function (done) {
return gulp.src('./coverage/merged/lcov.info')
.pipe(plugins.coveralls());
});

// Karma test runner task
Expand Down Expand Up @@ -404,6 +447,10 @@ gulp.task('test:e2e', function (done) {
runSequence('env:test', 'lint', 'dropdb', 'nodemon', 'protractor', done);
});

gulp.task('test:coverage', function (done) {
runSequence('env:test', ['copyLocalEnvConfig', 'makeUploadsDir', 'dropdb'], 'lint', 'configure-coverage', 'mocha:coverage', 'karma', 'coveralls', done);
});

// Run the project in development mode
gulp.task('default', function (done) {
runSequence('env:dev', ['copyLocalEnvConfig', 'makeUploadsDir'], 'lint', ['nodemon', 'watch'], done);
Expand Down
3 changes: 1 addition & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ module.exports = function (karmaConfig) {
dir: 'coverage/client',
reporters: [
// Reporters not supporting the `file` property
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'report-lcov' },
{ type: 'lcov', subdir: '.' },
// Output coverage to console
{ type: 'text' }
],
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"test:server:watch": "gulp test:server:watch",
"test:client": "gulp test:client",
"test:e2e": "gulp test:e2e",
"test:coverage": "gulp test:coverage",
"postinstall": "bower install --allow-root && bower prune --allow-root",
"generate-ssl-certs": "scripts/generate-ssl-certs.sh"
},
Expand All @@ -49,7 +50,6 @@
"glob": "~7.0.0",
"grunt": "~1.0.1",
"grunt-cli": "~1.2.0",
"gulp-node-inspector": "~0.1.0",
"helmet": "~2.1.2",
"jasmine-core": "~2.4.1",
"lodash": "~4.15.0",
Expand Down Expand Up @@ -102,15 +102,18 @@
"gulp-angular-templatecache": "~1.8.0",
"gulp-autoprefixer": "~3.1.0",
"gulp-concat": "~2.6.0",
"gulp-coveralls": "~0.1.4",
"gulp-csslint": "~0.2.0",
"gulp-csso": "~1.1.0",
"gulp-eslint": "~2.0.0",
"gulp-imagemin": "~2.4.0",
"gulp-istanbul": "~1.1.1",
"gulp-less": "~3.0.5",
"gulp-livereload": "~3.8.1",
"gulp-load-plugins": "~1.2.0",
"gulp-mocha": "~3.0.1",
"gulp-ng-annotate": "~1.1.0",
"gulp-node-inspector": "~0.1.0",
"gulp-nodemon": "~2.0.6",
"gulp-protractor": "~2.1.0",
"gulp-rename": "~1.2.2",
Expand All @@ -126,6 +129,7 @@
"karma-jasmine": "~0.3.7",
"karma-ng-html2js-preprocessor": "~0.2.1",
"karma-phantomjs-launcher": "~1.0.0",
"lcov-result-merger": "~1.2.0",
"load-grunt-tasks": "~3.4.1",
"mock-fs": "~3.11.0",
"node-inspector": "~0.12.7",
Expand Down

7 comments on commit 7be1910

@mleanos
Copy link
Member

@mleanos mleanos commented on 7be1910 Sep 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyperreality @lirantal I'm getting an error at the end of the coveralls run. Could this have been what was happening on the Travis builds with the PR?

gulp-coveralls-error

It looks like it's throwing an error with the client-side coverage. The client tests ran, but they couldn't be analyzed by coveralls. At least that's my hunch.

@hyperreality
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mleanos Hi, thanks for looking into this issue. I think you're getting that error because you're missing a .coveralls.yml file with a personal repo token in it.

@mleanos
Copy link
Member

@mleanos mleanos commented on 7be1910 Sep 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I get me one of them? :) And why is it required? If this can be automated, then we should look into adding a Gulp task to do this for the user. I would never have know, unless I dug deeper into Coveralls.

@hyperreality
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mleanos Good point. Perhaps we should split this into two separate gulp tasks then, one which simply runs the coverage locally and one which additionally submits the results to coveralls.io?

@mleanos
Copy link
Member

@mleanos mleanos commented on 7be1910 Sep 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's a good idea. We should make it configurable, so that a user can easily opt in/out of this functionality.

@hyperreality
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, should be a quick fix. I'll also look closer into the issue with client-side coverage, perhaps as @lirantal says it's to do with the .travis.yml file.

@lirantal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks guys.

Please sign in to comment.