简体   繁体   中英

server.close() doesn't work in a Vow teardown

I'm trying to write some Vows-based tests for my run-of-the-mill Express app.

Here's the test source:

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

And here's start-app.js :

var app = require('../../app.js');

function start() {
    var server = app.listen(56971, 'localhost');
    return server;
}

module.exports = start;

app.js exports a regular Express.js app, created with express() .

The problem is that whenever I run the test, topic.close() doesn't work in the teardown function, and the test hangs forever after succeeding. I've tried searching the web and adding lots and lots of console.log s, all to no avail.

I'm on the Windows x64 build of Node.js 4.2.0, and I'm using assert@1.3.0 and vows@0.8.1 .

Any idea how I can make my test stop hanging?

Here's what I did to solve the issue in a project I was contributing: a final batch just to close the server.

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

Before adding this test, suite would never end executing. You can check the results at https://travis-ci.org/fmalk/node-static/builds/90381188

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM