Trouble working with chainsaw and mocha #5

Open
NickHeiner opened this Issue Jul 29, 2013 · 0 comments

Projects

None yet

1 participant

Maybe I'm just doing something silly, but I'm having a hard time getting chainsaw to behave as expected with mocha.

The following code behaves as expected:

var chainsaw = require('chainsaw'),
    expect = require('chai').expect;

describe('', function() {
    it('', function() {

        var ch = chainsaw(function (saw) {
            var num = 0;

            this.get = function (cb) {
                cb(num);
                saw.next();
            };

            this.set = function (n) {
                num = n;
                saw.next();
            };

            saw.on('end', function () {
                console.log('ch.js end');
            });
        });

        var times = 0;
        ch
            .get(function (x) {
                console.log('ch.js get ' + x)
                times ++;
            })
            .set(10)
            .get(function (x) {
                console.log('ch.js get ' + x)
                times ++;
            })
            .set(20)
            .get(function (x) {
                console.log('ch.js get ' + x)
                times ++;
            })
        ;

    });
});

All the log statements are hit:

ch.js get 0
ch.js get 10
ch.js get 20
ch.js end

However, moving the creation of ch outside the describe block makes the callbacks passed to get never get executed.

var chainsaw = require('chainsaw'),
    expect = require('chai').expect;

var ch = chainsaw(function (saw) {
    var num = 0;

    this.get = function (cb) {
        cb(num);
        saw.next();
    };

    this.set = function (n) {
        num = n;
        saw.next();
    };

    saw.on('end', function () {
        console.log('ch.js end');
    });
});

describe('', function() {
    it('', function() {

        var times = 0;
        ch
            .get(function (x) {
                console.log('ch.js get ' + x)
                times ++;
            })
            .set(10)
            .get(function (x) {
                console.log('ch.js get ' + x)
                times ++;
            })
            .set(20)
            .get(function (x) {
                console.log('ch.js get ' + x)
                times ++;
            })
        ;

    });
});

No console statements from within get and set are run, but the on('end') one is:

ch.js end

What's going on here? Am I doing something dumb?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment