title: Karma - TypeError: Cannot read property 'on' of undefined (alternative 'task completion callback called too many times' in runsequence)
body: <p>Running the following gulp task to start a karma server:</p>
  
  <pre><code>gulp.task('unit-test', function () {
      // Be sure to return the stream
      return gulp.src(['random text because gulp is weird'])
              .pipe(debug({verbose: true}))
              .pipe(new Server({
                      configFile: __dirname + '/test/unit/karma.conf.js',
                      singleRun: true
                    })
                    .start())
              .on('error', function (err) {
                  throw err;
              });
  });
  </code></pre>
  
  <p>throws some errors:</p>
  
  <pre><code>[17:24:50] 'unit-test' errored after 81 ms
  [INFO] [17:24:50] TypeError: Cannot read property 'on' of undefined
  [INFO]     at DestroyableTransform.Readable.pipe (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp-debug/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:516:7)
  [INFO]     at Gulp.&lt;anonymous&gt; (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/gulpfile.js:185:14)
  [INFO]     at module.exports (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
  [INFO]     at Gulp.Orchestrator._runTask (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
  [INFO]     at Gulp.Orchestrator._runStep (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
  [INFO]     at Gulp.Orchestrator.start (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
  [INFO]     at runNextSet (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/run-sequence/index.js:79:16)
  [INFO]     at Gulp.onTaskEnd (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/run-sequence/index.js:68:5)
  [INFO]     at Gulp.emit (events.js:129:20)
  [INFO]     at Gulp.Orchestrator._emitTaskDone (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:264:8)
  [INFO]     at /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:275:23
  [INFO]     at finish (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
  [INFO]     at cb (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:29:3)
  [INFO]     at finish (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/run-sequence/index.js:53:5)
  [INFO]     at runNextSet (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/run-sequence/index.js:81:5)
  [INFO]     at Gulp.onTaskEnd (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/run-sequence/index.js:68:5)
  [INFO] [17:24:50] 'clean-build-test' errored after 290 ms
  [INFO] [17:24:50] Error: [object Object]
  [INFO]     at formatError (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/bin/gulp.js:169:10)
  [INFO]     at Gulp.&lt;anonymous&gt; (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/bin/gulp.js:195:15)
  [INFO]     at Gulp.emit (events.js:129:20)
  [INFO]     at Gulp.Orchestrator._emitTaskDone (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:264:8)
  [INFO]     at /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:275:23
  [INFO]     at finish (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
  [INFO]     at cb (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:29:3)
  [INFO]     at finish (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/run-sequence/index.js:53:5)
  [INFO]     at Gulp.onError (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/run-sequence/index.js:60:4)
  [INFO]     at Gulp.emit (events.js:129:20)
  [INFO]     at Gulp.Orchestrator._emitTaskDone (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:264:8)
  [INFO]     at /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:275:23
  [INFO]     at finish (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
  [INFO]     at module.exports (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:36:10)
  [INFO]     at Gulp.Orchestrator._runTask (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
  [INFO]     at Gulp.Orchestrator._runStep (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
  [INFO] [17:24:50] gulp-debug: 0 items
  </code></pre>
  
  <p>Although interestingly the karma tests still pass:</p>
  
  <pre><code>...
  ...
  20 07 2015 17:25:34.934:DEBUG [karma]: List of files has changed, trying to execute
  20 07 2015 17:25:34.935:WARN [karma]: No captured browser, open http://localhost:9876/
  20 07 2015 17:25:34.937:DEBUG [watcher]: Watching "/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/webapp/js/lib.js"
  20 07 2015 17:25:34.938:DEBUG [watcher]: Watching "/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/webapp/js/app.js"
  20 07 2015 17:25:34.938:DEBUG [watcher]: Watching "/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/test/unit"
  20 07 2015 17:25:34.940:INFO [karma]: Karma v0.13.2 server started at http://localhost:9876/
  20 07 2015 17:25:34.943:INFO [launcher]: Starting browser PhantomJS
  20 07 2015 17:25:34.944:DEBUG [temp-dir]: Creating temp dir at /var/folders/43/yyskpwq91b3b_qjxpj5nsqkh0000gn/T/karma-97139922
  20 07 2015 17:25:34.946:DEBUG [launcher]: /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/phantomjs/lib/phantom/bin/phantomjs /var/folders/43/yyskpwq91b3b_qjxpj5nsqkh0000gn/T/karma-97139922/capture.js
  20 07 2015 17:25:35.982:DEBUG [web-server]: serving: /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma/static/client.html
  20 07 2015 17:25:35.991:DEBUG [web-server]: serving: /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma/static/karma.js
  20 07 2015 17:25:36.008:DEBUG [karma]: A browser has connected on socket Rwzn4QBMQ6mTGcBFAAAA
  20 07 2015 17:25:36.011:DEBUG [web-server]: upgrade /socket.io/?EIO=3&amp;transport=websocket&amp;sid=Rwzn4QBMQ6mTGcBFAAAA
  20 07 2015 17:25:36.028:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket Rwzn4QBMQ6mTGcBFAAAA with id 97139922
  20 07 2015 17:25:36.028:DEBUG [launcher]: PhantomJS (id 97139922) captured in 1.088 secs
  20 07 2015 17:25:36.029:DEBUG [karma]: All browsers are ready, executing
  20 07 2015 17:25:36.034:DEBUG [web-server]: serving: /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma/static/context.html
  20 07 2015 17:25:36.036:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma-chai-plugins/node_modules/sinon/pkg/sinon.js
  20 07 2015 17:25:36.037:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma-chai-plugins/function-bind-polyfill.js
  20 07 2015 17:25:36.038:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/webapp/js/lib.js
  20 07 2015 17:25:36.048:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/chai/chai.js
  20 07 2015 17:25:36.048:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma-chai-plugins/chai-adapter.js
  20 07 2015 17:25:36.048:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/mocha/mocha.js
  20 07 2015 17:25:36.049:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma-mocha/lib/adapter.js
  20 07 2015 17:25:36.050:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/test/unit/authenticationService.spec.js
  20 07 2015 17:25:36.050:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/test/unit/homepage.spec.js
  20 07 2015 17:25:36.050:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/chai-as-promised/lib/chai-as-promised.js
  20 07 2015 17:25:36.050:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma-chai-plugins/node_modules/sinon-chai/lib/sinon-chai.js
  20 07 2015 17:25:36.051:DEBUG [web-server]: serving (cached): /Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/webapp/js/app.js
  ....
  PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 4 of 4 SUCCESS (0.001 secs / 0.003 secs)
  </code></pre>
  
  <p>Attempting to follow the example more closely (adding a callback within the Server call):</p>
  
  <pre><code>gulp.task('unit-test', function (cb) {
      // Be sure to return the stream
      return gulp.src(['random text because gulp is weird'])
              .pipe(debug({verbose: true}))
              .pipe(new Server({
                      configFile: __dirname + '/test/unit/karma.conf.js',
                      singleRun: true
                    }, cb)
                    .start())
              .on('error', function (err) {
                  throw err;
              });
  });
  </code></pre>
  
  <p>leads to errors like:</p>
  
  <pre><code>21:48:36] Error: task completion callback called too many times
      at finish (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:15:10)
      at cb (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:29:3)
      at removeAllListeners (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma/lib/server.js:311:7)
      at Server.&lt;anonymous&gt; (/Users/karianna/Documents/workspace/AdoptOpenJDK_Projects/betterrev_project/betterrev/src/main/angularjsapp/node_modules/karma/lib/server.js:322:9)
      at Server.g (events.js:199:16)
      at Server.emit (events.js:129:20)
      at net.js:1419:10
      at process._tickCallback (node.js:355:11)
  </code></pre>
  
  <p>Which I think is because I've got nested callbacks (the task is part of a runsequence, i.e.)</p>
  
  <pre><code>gulp.task('clean-build-test', function (callback) { 
    runSequence('clean-build', 'unit-test', 'integration-test', callback); 
  });
  </code></pre>
  
  <p>I'm very much a noob at Node and Javascript so happy to be told to go away and provide better info :-).</p>