Skip to content

Instantly share code, notes, and snippets.

@renegare
Last active March 28, 2018 05:37
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renegare/9173656 to your computer and use it in GitHub Desktop.
Save renegare/9173656 to your computer and use it in GitHub Desktop.
Gulp Karma Integration
/**
* testing tasks (using karma to test in the browser). Requires a karma.conf.js for full config
* single-run testing
* continuous testing
*/
/** base deps, but you may need more specifically for your application */
var gulp = require('gulp');
var gutil = require('gulp-util');
var path = require('path');
var karma = require('karma');
var karmaParseConfig = require('karma/lib/config').parseConfig;
function runKarma(configFilePath, options, cb) {
configFilePath = path.resolve(configFilePath);
var server = karma.server;
var log=gutil.log, colors=gutil.colors;
var config = karmaParseConfig(configFilePath, {});
Object.keys(options).forEach(function(key) {
config[key] = options[key];
});
server.start(config, function(exitCode) {
log('Karma has exited with ' + colors.red(exitCode));
cb();
process.exit(exitCode);
});
}
/** actual tasks */
/** single run */
gulp.task('test', function(cb) {
runKarma('karma.conf.js', {
autoWatch: false,
singleRun: true
}, cb);
});
/** continuous ... using karma to watch (feel free to circumvent that;) */
gulp.task('test-dev', function(cb) {
runKarma('karma.conf.js', {
autoWatch: true,
singleRun: false
}, cb);
});
@renegare
Copy link
Author

Note: This was built specifically for my needs but turns out to be generic enough to be used widely. Most of your configuration and use of karma should be done in karam e.g. [your]karma.conf.js.

@jeef3
Copy link

jeef3 commented Jun 21, 2014

Life saver! 👍

@mattymess
Copy link

Thanks so much for this. I really hated having my karma config inside my gulpfile!

@sebald
Copy link

sebald commented Sep 24, 2015

You should uppercase the server, because otherwehise you'll use the "old server" :)

var server = karma.server; -> var server = karma.Server;

Current export statement of karma:

module.exports = {
  VERSION: constants.VERSION,
  Server: Server,
  runner: runner,
  launcher: launcher,
  server: oldServer
}

@activedecay
Copy link

exactly what I needed. awesome!

@vijayreddygudi
Copy link

This helped a lot! thanks.

@drinkbird
Copy link

Thanks! Really helpful

@Carniatto
Copy link

Thank you, this is very useful.

But I think you should update now that karma.server has been deprecated

@aidanhmiles
Copy link

👍

@jeremyGoupil
Copy link

👍

@felipecodes
Copy link

Thank you!!! 🤘

@shaharkazaz
Copy link

Great Job! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment