Javascript-Karma-TypeError:无法读取未定义的属性“ on”(在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)

尝试更紧密地遵循该示例(在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,因此很高兴被告知离开并提供更好的信息:-).

解决方法:

一般来说,Gulp仅负责创建和引导流.同时,每个中间人,就像您的新服务器调用一样,都负责从管道函数返回流.我来自Java,我认为在类型系统的上下文中进行理解最容易.将Gulp视为实现以下内容的类:

interface GulpTask {
  Stream pipe(Stream stream);
}

当到达调用新服务器的功能时,您会收到一个流,但不返回一个流.实际上,您不会返回使默认值未定义的任何内容.因此,错误消息通知您无法调用未定义的消息.这就像您实现了:

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

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

相反,只有在没有错误发生的情况下(如果新的Server管道返回了流),才会附加代码的错误处理程序.这也是运行测试但最终收到错误处理程序的原因.错误处理程序仅在处理先前的管道之后才附加.

也许您正在尝试将错误处理程序附加到Karma服务器?然后,您需要在新创建的服务器而不是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跟踪的回调,以便与您的运行序列进行互操作.

上一篇:每日一练python49


下一篇:Python壁球小游戏(展示型)与图像的基本使用