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