karma-runner / gulp-karma

Example of using Karma with Gulp.
310 stars 43 forks source link

Example calls back too many times #22

Open bennett000 opened 9 years ago

bennett000 commented 9 years ago

The given example seems to end properly, but eventually calls back on my end too many times with the following error: Error: task completion callback called too many times. I would guess this is related to me using multiple browsers?

I was able to stop the issue by listening to Karma's run_complete event, and firing gulp's done at that point, instead of through the Karma Server constructor's callback.

This is not really an "issue", but this might be useful to someone else.

Current Example:

var gulp = require('gulp');
var Server = require('karma').Server;

/**
 * Run test once and exit
 */
gulp.task('test', function (done) {
  new Server({
    configFile: __dirname + '/karma.conf.js',
    singleRun: true
  }, done).start();
});

More or less produces on my end:

PhantomJS 1.9.8 (Linux 0.0.0): Executed 23 of 23 SUCCESS (0.087 secs / 0.079 secs)
Firefox 40.0.0 (Ubuntu 0.0.0): Executed 23 of 23 SUCCESS (0.01 secs / 0.064 secs)
TOTAL: 46 SUCCESS
KARMA DONE
PhantomJS 1.9.8 (Linux 0.0.0): Executed 23 of 23 DISCONNECTED (10.094 secs / 0.079 secs)
Firefox 40.0.0 (Ubuntu 0.0.0): Executed 23 of 23 SUCCESS (0.01 secs / 0.064 secs)
KARMA DONE
[12:51:08] 'test-karma' errored after 11 s
[12:51:08] Error: task completion callback called too many times

("KARMA DONE" was console.logged out, and the task was named "test-karma" instead of just "test")

Updated (Working For Me) Example

Skimming through the Karma docs I updated the example to throw on browser error, and check Karma's results.

I'm very new to gulp so this might be all wrong, but it completes as expected on my end.

/*global require*/
var gulp = require('gulp'),
    gutil = require('gulp-util'),
    Server = require('karma').Server;

gulp.task('test', function (done) {
    var server =  new Server({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    });

    server.on('browser_error', function (browser, err){
        gutil.log('Karma Run Failed: ' + err.message);
        throw err;
    });

    server.on('run_complete', function (browsers, results){
        if (results.failed) {
            throw new Error('Karma: Tests Failed');
        }
        gutil.log('Karma Run Complete: No Failures');
        done();
    });

    server.start();
});
sirhodes commented 8 years ago

thank you @bennett000, this worked for me! A PR to update the documentation on this one might help out a lot of people.

arizonatribe commented 8 years ago

Likewise, thanks @bennett000, this helped me out too.

ViieeS commented 7 years ago

@bennett000 chaining :grin:

/*global require*/
var gulp = require('gulp'),
    gutil = require('gulp-util'),
    Server = require('karma').Server;

gulp.task('test', function (done) {
    new Server({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }).on('browser_error', function (browser, err){
        gutil.log('Karma Run Failed: ' + err.message);
        throw err;
    }).on('run_complete', function (browsers, results){
        if (results.failed) {
            throw new Error('Karma: Tests Failed');
        }
        gutil.log('Karma Run Complete: No Failures');
        done();
    }).start();
});