Skip to content

Instantly share code, notes, and snippets.

@renegare
Last active March 28, 2018 05:37
Show Gist options
  • Select an option

  • Save renegare/9173656 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
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

jeef3 commented Jun 21, 2014

Copy link
Copy Markdown

Life saver! πŸ‘

@mattymess

Copy link
Copy Markdown

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

@sebald

sebald commented Sep 24, 2015

Copy link
Copy Markdown

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
Copy Markdown

exactly what I needed. awesome!

@vijayreddygudi

Copy link
Copy Markdown

This helped a lot! thanks.

@drinkbird

Copy link
Copy Markdown

Thanks! Really helpful

@Carniatto

Copy link
Copy Markdown

Thank you, this is very useful.

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

@aidanhmiles

Copy link
Copy Markdown

πŸ‘

@jeremyGoupil

Copy link
Copy Markdown

πŸ‘

@felipecodes

Copy link
Copy Markdown

Thank you!!! 🀘

@shaharkazaz

Copy link
Copy Markdown

Great Job! Thanks!

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