vows unit test got executed multiple times when the included app server uses nodejs cluster.fork

My app server uses node.js cluster API cluster.fork() to fork multiple child processes. This works fine.

However, when I try to use vows for unit test, the test also got run multiple times because of the call to cluster.fork() inside my app server; as I instantiate the server inside my test code, as follows:

basic-test.js

var vows = require('vows');
var MyAppServer = require('../my_app');

// start the server 
var app = MyAppServer.start();    

var suite = vows.describe('My Tests');
suite.discuss('connections API')
    .addBatch({ ... })
    .export(module);

How do I prevent test code to run multiple times in this case ? This test is included in npm test, so I need a way to instantiate my app server inside test itself.

At the top you can do

var cluster = require('cluster');

Then wrap the suite in an if:

if (cluster.isMaster) {
    var suite = ...
    ...
}

For more info on isMaster, check the documentation