Karma - TypeError: Cannot read property 'on' of undefined (alternative 'task completion callback called too many times' in runsequence)

Karma - TypeError: Cannot read property 'on' of undefined (alternative 'task completion callback called too many times' in runsequence)

运行 以下 gulp 任务启动业力服务器:

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;
            });
});

抛出一些错误:

[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.<anonymous> (/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.<anonymous> (/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

虽然有趣的是业力测试仍然通过:

...
...
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&transport=websocket&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)

尝试更紧密地遵循示例(在服务器调用中添加回调):

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;
            });
});

导致如下错误:

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.<anonymous> (/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)

我认为这是因为我有嵌套回调(任务是运行序列的一部分,即)

gulp.task('clean-build-test', function (callback) { 
  runSequence('clean-build', 'unit-test', 'integration-test', callback); 
});

我是 Node 的菜鸟,Javascript 很高兴被告知离开并提供更好的信息:-)。

gulp.task('unit-test', function (cb) {
  new Server({
    configFile: __dirname + '/test/unit/karma.conf.js',
    singleRun: true
  }, cb).start();
});

一般来说,Gulp只负责创建和导流。同时,每个 中间人 ,就像您的 new Server 调用一样,负责 return 来自 pipe 函数的流。来自 Java,我认为在类型系统的上下文中思考是最容易理解的。将 Gulp 想象成 class 实现:

interface GulpTask {
  Stream pipe(Stream stream);
}

当到达您调用 new Server 的函数时,您会收到一个流,但您不会 return 一个。事实上,您不会 return 任何 return 值默认为 undefined 的东西。因此,错误消息通知您不能在 undefined 上调用 on。这就像您实现了:

class BrokenStream implements GulpTask {
  @Override
  public Stream pipe(Stream stream) {
    new Server(<something>);
    return null;
  }
}

发生这种情况的原因是您直接使用 karma 而不知道 Gulp 的流。通常,这不是问题,因为在管道的末端使用了 Karma。

同时,您的错误处理程序没有生效。您需要在潜在应用程序之前注册错误处理程序,即:

gulp.task('unit-test', function () {
  return gulp
    .src(['https://www.youtube.com/watch?v=6Xywqv1cDH8'])
    .on('error', function (err) { <do something> }) // Returns a handled stream.
    .pipe(<something>); // The received stream now has an error handler.
});

这样,您的流在被下一个管道指令处理时附加了一个错误处理程序。一旦以下任何管道指令发出错误,就会触发此处理程序。

相比之下,您的代码的错误处理程序只会在没有错误发生的情况下附加(假定 new Server 管道已经 return 编辑了一个流)。这也是为什么您的测试是 运行 但您最终会收到错误处理程序的原因。错误处理程序仅在之前的管道处理之后附加。

也许您正在尝试将错误处理程序附加到您的 Karma 服务器?然后,您需要在新创建的服务器上使用不同的参数调用 on,并在 Gulp 流上调用 notofficial Karma API 告诉你怎么做。

至于将回调附加到 Karma 服务器:我假设您以此为起点:https://github.com/karma-runner/gulp-karma

请注意,您是在流中使用 Karma 服务器。这不是您通常的做法。

使用管道,Karma 服务器正在接收每个单独的测试并为每个单独的测试触发创建!同时,一旦每个单独的测试有 运行,每个服务器实例都会收到整个任务的完成回调。因此,您收到第二条错误消息。

您可以通过执行自己的简单步骤来跟踪此行为:

var mapStream = require('map-stream');

gulp.task('unit-test', function () {
  return gulp.src(['random text because gulp is weird'])
    .pipe(debug({verbose: true}))
    .pipe(mapStream(function (file, cb) {
      console.log(file);
      return cb(null, file)
    }))
    .pipe(new Server({
      configFile: __dirname + '/test/unit/karma.conf.js',
      singleRun: true
    }).start())
}

我想知道您为什么要为此目的使用流。只需将 Karma 服务器定义为它自己的任务,就像在前一个插件的示例中所做的那样。或者,您需要像这样包装您的服务器:

.pipe(mapStream(function (file, cb) {
  new Server({
    configFile: __dirname + '/test/unit/karma.conf.js',
    singleRun: true
  }, cb).start()
 }))

这样,服务器会触发文件步骤的回调,而不是任务之一,您的应用程序也会 return 一个可以被 Gulp 跟踪的回调,以便与您的 运行序列。