Karma - TypeError:无法读取未定义的属性'on'(在runsequence中调用太多次的替代'任务完成回调)

时间:2015-07-20 16:35:38

标签: javascript node.js gulp karma-runner

运行以下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)

尝试更密切地关注示例(在Server调用中添加回调):

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的菜鸟很高兴被告知离开并提供更好的信息: - )。

2 个答案:

答案 0 :(得分:4)

一般来说,Gulp只负责创建和指导流。与此同时,每个中间人new Server - 调用一样,负责从pipe函数返回流。来自Java,我认为在类型系统的上下文中最容易理解这一点。把Gulp想象成一个实现的类:

interface GulpTask {
  Stream pipe(Stream stream);
}

当您到达调用new Server的函数时,您会收到一个流,但您不会返回一个。事实上,您不会返回任何返回值默认为undefined的内容。因此,错误消息通知您无法在on上致电undefined。这就好像你实现了:

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

发生这种情况的原因是你直接使用了不知道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管道返回了一个流),才会附加代码的错误处理程序。这也是运行测试的原因,但最终会收到错误处理程序。错误处理程序仅在处理之前的管道后附加

也许您正在尝试将错误处理程序附加到您的Karma服务器?然后,您需要在新创建的服务器上使用不同的参数调用on,并在Gulp流上调用official 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()
 }))

这样,服务器会触发文件步骤的回调,而不是任务之一,您的应用程序也会返回一个回调,Gulp可以跟回该回调,以便与您的运行顺序互操作。

答案 1 :(得分:0)

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