Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7931619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:46:21+00:00 2026-06-03T20:46:21+00:00

I’ve got a set of modules that run based on a global event emitter.

  • 0

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:

  1. boot.ready
  2. server created (because of boot.ready event)
  3. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:46:24+00:00Added an answer on June 3, 2026 at 8:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.