nodejs and TAPE integration issues. App initialization call gets blocked

390 Views Asked by At

I am creating unit tests for nodejs app using "tape". When I run tests, the process gets blocked forever. When I dug deeper, I found that if I run "nodejs mainapp.js" the process doesn't end. That's also blocking running of unit tests.

Looks like nodejs process will not end if there are events registered. In my case, creating a mongo or redis connection or use of node modules such as "node-cache" results into this. It's hard to imagine that all such modules can be avoided. Any suggestion on how to get around the problem. My test case is really simple so there are on issues there.

By the way, I have verified nodejs doesn't get blocked due to http.listen. Someone could argue about using mockups, I am not looking to do that.

test('dummy test', function(t) {
  t.equal('hello world', 'hello world', 'Yes hello world = hellow world');
  t.end();
});
1

There are 1 best solutions below

0
On

The main cause of tape processes keeping alive are MongoDB connections not closed. The easiest way to close a connection when all tests ends, is to create a blank test with instructions to close the MongoDB connection. The following is an example, in this case using Mongoose:

var test = require('tape');
var mongoose = require('mongoose');

test('test', function (t) {
    t.pass('Example');
    t.end();
});

test('finish', function (t) {
    mongoose.disconnect();
    t.pass('closing db connection');
    t.end();
});

if the problem is indeed a MongoDB connection, a more proper way to deal with it would be using test.onFinish(fn) according to documentation:

The onFinish hook will get invoked when ALL tape tests have finished right before tape is about to print the test summary.