Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue running karma task from gulp

I am trying to run karma tests from gulp task and I am getting this error:

Error: 1
   at formatError (C:\Users\Tim\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:161:10)
   at Gulp.<anonymous> (C:\Users\Tim\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:187:15)
   at Gulp.emit (events.js:95:17)
   at Gulp.Orchestrator._emitTaskDone (C:\path\to\project\node_modules\gulp\node_modules\orchestrator\index.js:264:8)
   at C:\path\to\project\node_modules\gulp\node_modules\orchestrator\index.js:275:23
   at finish (C:\path\to\project\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8)
   at cb (C:\path\to\project\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:29:3)
   at removeAllListeners (C:\path\to\project\node_modules\karma\lib\server.js:216:7)
   at Server.<anonymous> (C:\path\to\project\node_modules\karma\lib\server.js:227:9)
   at Server.g (events.js:180:16)

My system is Windows 7, nodejs version is v0.10.32, gulp version:

[10:26:52] CLI version 3.8.8
[10:26:52] Local version 3.8.9

Also, the same error I am getting on Ubuntu 12.04 LTS while on newer Ubuntu (not sure what version) and mac os it is seems to be working ok. What can cause this error?

Update 5/11/2016: Before writing comment about the fact that accepted answer hide errors, please, see first two comments to that particular accepted answer. Use it only if know what you are doing. Related info: https://github.com/karma-runner/gulp-karma/pull/15

like image 497
Timur Avatar asked Oct 28 '14 17:10

Timur


3 Answers

None of these solutions worked correctly for me using gulp 3.9.1 and karma 1.1.1. Adding a reference to gulp-util npm install --save-dev gulp-util and updating the task to the below fix the error output very nicely, while maintaining exit status correctly.

var gutil = require('gulp-util');

gulp.task('test', function (done) {
  new Server({
    configFile: __dirname + '/karma.conf.js',
    singleRun: true
  }, function(err){
        if(err === 0){
            done();
        } else {
            done(new gutil.PluginError('karma', {
                message: 'Karma Tests failed'
            }));
        }
    }).start();
});
like image 45
brocksamson Avatar answered Nov 20 '22 03:11

brocksamson


How are you running your tests with Gulp? I came up against this issue recently on OSX, running node v0.11.14 and gulp 3.8.10, whenever there were failing tests.

Changing from the recommended:

gulp.task('test', function(done) {
    karma.start({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }, done);
});

To:

gulp.task('test', function(done) {
    karma.start({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }, function() {
        done();
    });
});

...got rid of this error.

Seems to be down to how gulp handles error messages when an error is signalled in a callback. See Improve error messages on exit for more information.

like image 126
McDamon Avatar answered Nov 20 '22 03:11

McDamon


Below is a code snippet from gulp patterns on using Karma. It's a bit similar, but also uses the newer method how to start the karma.

/**
 * Start the tests using karma.
 * @param  {boolean} singleRun - True means run once and end (CI), or keep running (dev)
 * @param  {Function} done - Callback to fire when karma is done
 * @return {undefined}
 */
function startTests(singleRun, done) {
    var child;
    var excludeFiles = [];
    var fork = require('child_process').fork;
    var KarmaServer = require('karma').Server;
    var serverSpecs = config.serverIntegrationSpecs;

    if (args.startServers) {
        log('Starting servers');
        var savedEnv = process.env;
        savedEnv.NODE_ENV = 'dev';
        savedEnv.PORT = 8888;
        child = fork(config.nodeServer);
    } else {
        if (serverSpecs && serverSpecs.length) {
            excludeFiles = serverSpecs;
        }
    }

    var server = new KarmaServer({
        configFile: __dirname + '/karma.conf.js',
        exclude: excludeFiles,
        singleRun: singleRun
    }, karmaCompleted);
    server.start();

    ////////////////

    function karmaCompleted(karmaResult) {
        log('Karma completed');
        if (child) {
            log('shutting down the child process');
            child.kill();
        }
        if (karmaResult === 1) {
            done('karma: tests failed with code ' + karmaResult);
        } else {
            done();
        }
    }
}
like image 5
Igor Lino Avatar answered Nov 20 '22 03:11

Igor Lino