Mocha and generators

Lately I have been writing a dependency injection module for node.js which works with coroutines. I'll probably write more about it later since I find this topic super interesting but for now, one can look at this introduction to generators. I am using the excellent bluebird promises library for the coroutine part.

So let's try to write a mocha test with coroutines, there are a few gotcha.

Here's our basic generator:

function* generator() {
  yield 'ok';
  yield Promise.delay('late', 100);

(notice the * at the end). Here's how to use it (node >= 0.11.4 with --harmony flag):

var g = generator();; // { value: ok, done: false }; // { value: <promise object>, done: false }; // { value: undefined, done: true }

A basic mocha test would be:

suite('Async with generator', function() {
  test('basic generator', function() {
    var g = generator();
    assert.equal(, 'ok');
    assert.equal(, 'late');

And this fails... Because the second call to returns the promise object. A fix would be:

test('With synchronous yield', function(done) {
  var g = generator();
  assert.equal(, 'ok'); {
    assert.equal(val, 'late');

This pass, but is not very pretty, and suddendly, we have to get back the asynchronous callback back :(

A better solution is to use a [coroutine](