server.close() 在 Vow 拆解中不起作用



我正在尝试为我的普通Express应用程序编写一些基于誓言的测试。

下面是测试源:

var vows = require('vows');
var assert = require('assert');
var startApp = require('./lib/start-app.js');
var suite = vows.describe('tournaments');
suite.addBatch({
    "When we setup the app": {
        topic: function() {
            return startApp();
        },
        teardown: function(topic) {
            if (topic && topic.close) {
                topic.close();
            }
        },
        "it works": function(topic) {
            assert.isObject(topic);
        }
    }
});
suite.run();

这是start-app.js:

var app = require('../../app.js');
function start() {
    var server = app.listen(56971, 'localhost');
    return server;
}
module.exports = start;

app.js导出一个正则Express.js应用程序,由express()创建。

问题是,每当我运行测试时,topic.close()在teardown功能中不起作用,并且测试成功后永远挂起。我试着在网上搜索并添加了很多很多的console.log s,但都无济于事。

我在Windows x64版本的Node.js 4.2.0上,我使用assert@1.3.0vows@0.8.1

有什么办法可以让我的测试停止挂起吗?

在我参与的一个项目中,我是这样解决这个问题的:最后一批只是关闭服务器。

suite.addBatch({
  'terminate server': {
    topic: function() {
      server.close(this.callback); // this is a regular node require(`http`) server, reused in several batches
    },
    'should be listening': function() {
      /* This test is necessary to ensure the topic execution.
       * A topic without tests will be not executed */
      assert.isTrue(true);
    }
  }
}).export(module);

在添加此测试之前,suite将永远不会停止执行。您可以在https://travis-ci.org/fmalk/node-static/builds/90381188

查看结果

最新更新