可能告诉nodeunit在调用test.done()之前不要完成特定的testing?

我正在做一些与nodeunit的asynchronoustesting,我想知道是否有可能告诉nodeunit在test.done被调用之前不终止testing用例。

基本上这是我的testing用例现在的样子:

exports.basic = testCase({ setUp: function (callback) { this.ws = new WrappedServer(); this.ws.run(PORT); callback(); }, tearDown: function (callback) { callback(); }, testFoo: function(test) { var socket = ioClient.connect(URL); socket.emit('PING', 1, 1); socket.on('PONG', function() { // do some assertion of course test.done(); }); } }); 

现在的问题是,PONG不能够快速地被发送回来,以便testing代码被执行。 有任何想法吗?

我刚刚有一个非常类似的问题,因此我正在浏览这个问题。 在我的情况下,服务器(类似于你的WrappedServer)抛出一个exception,导致testing突然退出,没有用test.done()命中我的事件处理程序。 我认为这是非常粗鲁的nodeunit吞下exception没有窥视。

我不得不求助于debugging器来发现问题,如果以前没有做过,我可以为你保存一个websearch:node –debug -brk node_modules / nodeunit / bin / nodeunit your_nodeunit_test.js

问题是nodeunit不expect任何断言,所以它不会等待它们并立即终止。 计算您的断言并在testing开始时调用test.expect()

 exports.example = function(test) { // If you delete next line, the test will terminate immediately with failure. test.expect(1); setTimeout(function(){ test.ok(true); test.done(); }, 5000); }; 

当nodeunit显示“Undone tests”时,表示节点进程已经退出,没有完成所有的testing。 要清楚的是,这并不意味着“PONG不够快”,这意味着事件循环中没有更多的处理程序。 如果没有更多的处理者,那么PONG事件就不存在了,所以testing不可能继续下去。

例如,如果你跑这样的东西:

 var s = require('http').createServer(); s.listen(80) 

当您运行listen ,服务器开始监听传入数据,并将其添加到事件循环中以检查传入连接。 如果你只做了createServer,那么没有事件会触发,你的程序将会退出。

你有什么事情绑定到一个error事件,可能会使错误不显示?

你可能想要像这样的东西:

 /** Invoke a child function safely and prevent nodeunit from swallowing errors */ var safe = function(test, x) { try { x(test); } catch(ex) { console.log(ex); console.log(ex.stack); test.ok(false, 'Error invoking async code'); test.done(); } }; exports.testSomething = function(test){ test.expect(1); // Prevent early exit safe(test, function(test) { // ... some async code here }); };