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.

var Promise = require('bluebird');
test('With synchronous yield', Promise.coroutine(function* () {
  var g = generator();
  assert.equal(, 'ok');
  assert.equal(yield, 'late');

Much nicer !

Be careful to one gotcha though, everything running inside the coroutine is actually wrapped inside a promise. So if there is any exception inside, it won't by default be thrown outside. For example:

test('Promise rejected', Promise.coroutine(function* () {
  throw new Error('boom');

This test will pass. You can add logging with

  console.log('unhandled rejection detected !');

I haven't find yet a good enough solution to this problem, where I can have a failing test if an unexpected excepction is thrown.

If anyone has an idea ?