I have a set of modules that run based on the global event emitter. They work based on a chronological sequence of events, for example:
- boot.ready
- server created (due to boot.ready event)
- server configured (due to server.created event)
As such, I need to create server-test.js that runs the tests in chronological order.
Is this possible with Mocha? Something like the following?
var EventEmitter2 = require('eventemitter2').EventEmitter2, should = require('should'); describe('server', function() { var mediator = new EventEmitter2({ wildcard: false }); require('../../src/routines/server/creator')(mediator); require('../../src/routines/server/configurer')(mediator); it('should be created after boot', function(done) { mediator.once('server.created', function(server) { server.should.exist; done(); }); it('should be configured after created', function(done) { mediator.once('server.configured', function() { done(); }); }); mediator.emit('boot.ready'); }); });
Since there seems to be some kind of confusion about how this global event emitter works, this is the server/creator.js
:
module.exports = function(mediator) { var express = require('express'); mediator.once('boot.ready', function() { var server = express.createServer();
As you can see, the server is created after boot.ready
. This is triggered by server.created
, after which the configurator is launched, which then runs server.configured
.
This chain of events should be checked wet.
source share