Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Automatic test.done() call in asynchronous tests #289

Open
aleskafka opened this issue Dec 31, 2014 · 0 comments
Open

Feature request: Automatic test.done() call in asynchronous tests #289

aleskafka opened this issue Dec 31, 2014 · 0 comments

Comments

@aleskafka
Copy link

Nodeunit has great feature in expect, but ending asynchronous tests is often painful, mainly in tests with more than 1 callback. I think, it has easy solution and implementation isn't hard.

Instread of passing raw callback, pass returned callback from test.callback([expect 1,] callback);.

Invoking test.callback will store given callback and mark him as done after expected number of calls (internal mechanism will watch status of each callback). Each call will invoke original callback. If all test.callback are marked as done, test.done is called automatically.

It should handle all cases, even callback in callback.

It doesn't break existing code, it is optional.

Implementation could look like this:

test.callback = function(expected, callback) {
    if (typeof expected === 'function') {
        callback = expected;
        expected = 1;
    }

    this.callbacks = this.callbacks || 0;
    this.callbacks++;

    var test = this;
    var done = false;

    return function() {
        expected--;
        if (done===false && expected<=0) {
            done = true;
            test.callbacks--;
        }

        var ret = callback.apply(this, arguments);

        if (test.callbacks<=0) {
            test.done();
        }

        return ret;
    };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant