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

Commit

Permalink
feat(gulp): Gulp Server Watch for Mocha
Browse files Browse the repository at this point in the history
Adds a Gulp task that watches all server files (including server tests),
and upon any changes it will perform the Gulp *test:server* task.

Added a watch for server assets, to the main gulp watch config. This will only
add the watch when the NODE_ENV is set to "test".

Also, includes an **optional** argument for the task that can specify
that if the changed file is a test, then it will only run that test
file.

Example usage: gulp test:server:watch --onlyChanged
  • Loading branch information
mleanos committed Dec 17, 2015
1 parent b51e645 commit bf2eeed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
42 changes: 41 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var _ = require('lodash'),
defaultAssets = require('./config/assets/default'),
testAssets = require('./config/assets/test'),
glob = require('glob'),
gulp = require('gulp'),
gulpLoadPlugins = require('gulp-load-plugins'),
runSequence = require('run-sequence'),
Expand All @@ -16,6 +17,7 @@ var _ = require('lodash'),
}),
path = require('path'),
endOfLine = require('os').EOL,
argv = require('yargs').argv,
protractor = require('gulp-protractor').protractor,
webdriver_update = require('gulp-protractor').webdriver_update,
webdriver_standalone = require('gulp-protractor').webdriver_standalone,
Expand Down Expand Up @@ -66,6 +68,35 @@ gulp.task('watch', function () {
gulp.watch(defaultAssets.server.gulpConfig, ['jshint']);
gulp.watch(defaultAssets.client.views).on('change', plugins.livereload.changed);
}

if (process.env.NODE_ENV === 'test') {
// Add Server Test file rules
gulp.watch([testAssets.tests.server, defaultAssets.server.allJS], ['test:server']).on('change', function (file) {
var runOnlyChangedTestFile = argv.onlyChanged ? true : false;

// check if we should only run a changed test file
if (runOnlyChangedTestFile) {
var changedTestFiles = [];

// iterate through server test glob patterns
_.forEach(testAssets.tests.server, function (pattern) {
// determine if the changed (watched) file is a server test
_.forEach(glob.sync(pattern), function (f) {
var filePath = path.resolve(f);

if (filePath === path.resolve(file.path)) {
changedTestFiles.push(f);
}
});
});

// set task argument for tracking changed test files
argv.changedTestFiles = changedTestFiles;
}

plugins.livereload.changed();
});
}
});

// CSS linting task
Expand Down Expand Up @@ -181,13 +212,14 @@ gulp.task('templatecache', function () {
gulp.task('mocha', function (done) {
// Open mongoose connections
var mongoose = require('./config/lib/mongoose.js');
var testSuites = Array.isArray(argv.changedTestFiles) && argv.changedTestFiles.length ? argv.changedTestFiles : testAssets.tests.server;
var error;

// Connect mongoose
mongoose.connect(function () {
mongoose.loadModels();
// Run the tests
gulp.src(testAssets.tests.server)
gulp.src(testSuites)
.pipe(plugins.mocha({
reporter: 'spec',
timeout: 10000
Expand Down Expand Up @@ -275,6 +307,14 @@ gulp.task('test:server', function (done) {
runSequence('env:test', 'lint', 'mocha', done);
});

// Watch all server files for changes & run server tests (test:server) task on changes
// optional arguments:
// --onlyChanged - optional argument for specifying that only the tests in a changed Server Test file will be run
// example usage: gulp test:server:watch --onlyChanged
gulp.task('test:server:watch', function (done) {
runSequence('test:server', 'watch', done);
});

gulp.task('test:client', function (done) {
runSequence('env:test', 'lint', 'karma', done);
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"mock-fs": "~3.4.0",
"run-sequence": "^1.1.1",
"should": "^7.0.1",
"supertest": "^1.0.1"
"supertest": "^1.0.1",
"yargs": "~3.30.0"
}
}

0 comments on commit bf2eeed

Please sign in to comment.