Testing child_process.exec results in a flakey test… why?

Flakey Test

I am trying to write a unit test for a cucumber world module. The module seems to be working, it starts the Express application as a child_process and stops the app by killing the child process.

The issue is the mocha test for this module is flakey, it passes every other time and fails every other time. I can't seem to figure out why.

How can I write a test for the world file that will pass every time?

Cucumber World File
var zombie = require('zombie');
var cp = require('child_process');

var World = function World(callback) {
    this.browser = new zombie();

World.prototype.start = function (callback) {
    console.log("Starting Express App");
    this.app = cp.exec('node ./bin/www');
    console.log("Express App Started");
    if (callback) callback();

World.prototype.stop = function (callback) {
    this.app.on('close', function () {
        console.log("Stopped Express App");
        if (callback) callback();

World.prototype.visit = function (url, callback) {
    this.browser.visit(url, callback);

module.exports = World;
Flakey Test for the World Module
var assert = require('assert'),
http = require('http'),
World = require('../features/support/world');

describe("Cucumber World", function () {

    var world;

    beforeEach(function(done) {
        world = new World(function (world) {

    it('should start the web app', function (done) {
        http.get("http://localhost:3000", function (res) {
            assert.ok(res, "A successful response was made");
            assert.equal(res.statusCode, 200, "200 success status received");



Need Your Help

usage of …Pattern.compile()

java regex

Using java.util.regex.Pattern.compile() you can specify a pattern as parameter. But how can you specify flags like case-insensitive and ignore whitespaces as well?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.