how do I correctly access karma.config object in gulp configuration to watch files, then run karma, and finally build if karma passes?

StackOverflow https://stackoverflow.com/questions/23569367

Pregunta

Node noob here. After searching SO and google, I finally hacked together a gulp task that allows me to watch my files for changes to kick off karma tests and then if all tests pass, build my compiled js file. My solution is ugly and I'm wondering if anyone has any suggestions. If nothing else, someone may find this post useful if they're trying to accomplish the same thing.

Areas that could be improved:

  • Getting the configs out of my karma.conf.js
  • Somehow let karma.server continue running without having to kill it after every test run

Relevant gulpfile.js code:

var gulp = require('gulp');
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var karma = require('karma');

// clean task
gulp.task('clean', function() {
    return gulp
        .src('/build', { read : false })
        .pipe(clean());
});


gulp.task('build', ['clean'], function() {
    return gulp.src('/source/**/*.js')
        .pipe(concat('output.js'))
        .pipe(gulp.dest(target));
});


// karma testing
gulp.task('karma', function() {
    /* 
        this is a very hacky way to get to my karma configs.
        suggestions here greatly appreciated.
    */
    var configurator = require('./karma.conf.js'),
        // need to fake the configs object because i don't how to access the karma config object
        configs = {
            LOG_INFO: "INFO",
            set: function(options) {
                var i;

                for (i in options) {
                    this[i] = options[i];
                }
            }
        };
        configurator(configs);

    // overriding my karma.conf.js to just run once.
    configs.singleRun = true;

    karma.server.start(configs, function(exitCode) {

        if (exitCode == 0) {
            gulp.start('build');
        }

    });
});

gulp.task('watch', function() {
    gulp.watch('/source/**/*.js', ['karma']);
});
¿Fue útil?

Solución

It's not the prettiest, but I stumbled upon this, it actually works quite well: http://www.snip2code.com/Snippet/32023/Gulp-Karma-Integration

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top