How to carry out mocha tests in chronological order?

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(); //event: server created mediator.emit('server.created', server); }); }; 

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.

+4
source share
2 answers

If I am testing an event chain, a quick way is to nest calls eventEmitter.once as follows:

 it('executes in the right sequence', function(done) { mediator.once('boot.ready', function() { mediator.once('server.created', function() { done() }) }) }) 

edit: as indicated, server.created will be started before the boot.ready handler starts. Here is 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.

+2
source

In fact, mocha uses function.length for your it callbacks to know if you want them asynchronously, so with function(done) you cannot know in what order they are started. function() without a given argument will run them synchronously.

EDIT

Your pick is EventEmitter2, meaning that when you emit something, the handler will start async. As I said, they have no way of knowing the execution order.

The problem is with the necessary modules, each event should probably be emitted in the handler of the previous one. Code is better than words:

 // require('../../src/routines/server/creator')(mediator); // I guess this module creates the server, then it should emit server.created // require('../../src/routines/server/configurer')(mediator) // This one should listen to server.created then configure the server // and finally emit server.configured // So in it you should have something like this: mediator.once('server.created', function(server) { // Here you should configure you're server // Once it is configured then you emit server.configured }); 

You should also be aware that emit is immediately in node, so you better add your listeners before release.

Hope this is clear.

0
source

Source: https://habr.com/ru/post/1411844/


All Articles