I've already posted a few question the past few days, which were too long (I'm guessing because I didn't receive any non cryptic feedback). I've tried to make this brief.
The following code uses a 'setup-complete' event to notify the nodeunit setUp command to run the tests. Test 1 passes, test 2 fails with
FAILURES: Undone tests (or their setups/teardowns): - event based async code - test2
Is there some mistake in my code? Is nodeunit a bad choice for testing event based code? Is my approach? Any advice appreciated. thanks
async_setup.js:
var
events = require( 'events' ),
setup = new events.EventEmitter;
module.exports = function ( app, cb ) {
setup.on( 'setup-complete', function () {
cb();
});
setTimeout( function () {
if ( app.result ) throw new Error( "AlreadyConfiguredAppError" );
app.result = "app is configured";
setup.emit( 'setup-complete', app.result );
}, 5000 );
return app;
};
test/test.js:
var
nodeunit = require( 'nodeunit' ),
async_setup = require( '../async_setup' );
exports[ 'event based async code' ] = nodeunit.testCase({
setUp: function ( callback ) {
this.app = {};
async_setup( this.app, callback );
},
tearDown: function ( callback ) {
delete this.app;
callback();
},
'test1': function ( t ) {
t.expect( 1 );
t.ok( this.app.result !== undefined, 'app is configured' );
t.done();
},
'test2': function ( t ) {
t.expect( 1 );
t.ok( this.app.result !== undefined, 'app is configured' );
t.done();
}
});