How to run mocha tests in a chronological order?
asynchronous, javascript, mocha.js, node.js, unit-testing
Solution
If I'm testing a chain of events the quick way is to do it is nested eventEmitter.once calls like this:
it('executes in the right sequence', function(done) {
mediator.once('boot.ready', function() {
mediator.once('server.created', function() {
done()
})
})
})
edit: as pointed out server.created will be fired before the test's boot.ready handler is fired. Here's a workaround:
it('executes in the right sequence', function(done) {
var bootReadyFired = false
mediator.once('boot.ready', function() {
bootReadyFired = true
})
mediator.once('server.created', function() {
assert.ok(bootReadyFired)
done()
})
})
Hope this helps.
Problem
I've got a set of modules that run based on a global event emitter. They run based on a chronological chain of events, like so: - boot.ready - server created (because of boot.ready event) - server configured (because of server.created event) As such, I need to create a server-test.js that performs tests in a 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'); }); }); ``` Because there seemed to be some confusion about the way this global event emitter works, this is the `server/creator.js` module: ``` module.exports = function(mediator) { var express = require('express'); mediator.once('boot.ready', function() { var server = express.createServer(); //event: server created mediator.emit('server.created', server); }); }; ``` As you can see, the server is created after `boot.ready`. This fires `server.created`, after which the configurer will run which will then fire `server.configured`. This chain of events needs to be tested by mocha.