cucumber/cucumber-js — Issue #39: Support asynchronous world constructor
Author: salikh
Created: 2012-01-10 17:11:32+00:00
State: open

I am trying to use cucumber.js to test an express.js web application.
The test scenarios are creating new objects in MongoDb database,
and I need to clear it before running the tests.

Mongodb operations are asynchronous, so if I clear the database
from world constructor, then I have a race condition with test execution.

I came up with an idea how asynchronous initialization can be supported,
please see the fork at git://github.com/salikh/cucumber-js.git.

To request asynchronous initialization, the feature writer needs
to inherit from the EventEmitter by writing the following line:

this.__proto__ = new process.EventEmitter

Then the world constructor can indicate that it finished initialization
by firing an event on itself:

world.emit('init done')

The world initializer tests if the asynchronous initialization is
required by the following condition:

if (world.__proto__.constructor == process.EventEmitter)

and if the condition matches, it starts test execution asynchronously:

world.on('init done', function() {
cont()
})