title: Unit testing event driven javascript
body: <p>I'm writing a server-side application with node.js and would like to include unit tests. One thing I'm struggling with is a good way to unit test <code>EventEmitter</code> and <code>setInterval/setTimout</code></p>
  
  <p>What options do I have to unit test asynchronous server side javascript?</p>
  
  <p>I'm aware I can just attach another listener to the EventEmitter that is the testing function but then how do I garantuee that the testing function ever runs? part of the unit test is to ensure every part of the unit test runs. </p>
  
  <p>I could use setTimeout myself but that seems like a hackish solution.</p>
  
  <p>If it helps here is some exampler code I'm trying to test.</p>
  
  <pre><code>...
  function init(param) {
      ...
      // run update entities periodically
      setInterval(this._updateEntities.bind(this, param.containerFull),
          1000 / param.fps);
      ...
  }
  ...
  EntityUpdater.prototype = {
      ...
      "_updateEntities": function updateEntitiesfunc(fn) {
          this._tickEmitter.emit(
              "tick",
              new dataContainer.DataContainer(this.getEntityCount())
              .on(
                  "full", fn.bind(this)
              )
          );
      },
      ...
  }
  ...
  </code></pre>
  
  <p>(emit will trigger a event)</p>
  
  <p>[Edit]</p>
  
  <p>I started reading some of the EvevntEmitter tests at <a href="https://github.com/ry/node/tree/master/test/simple" rel="noreferrer">https://github.com/ry/node/tree/master/test/simple</a> and it helps me see how to go about this.</p>