caolan / caolan/nodeunit

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

Open
#289 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.9k
Forks
359
PR merge metrics
No merged PRs in 30d

Description

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:

``` javascript
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;
};
}
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the asynchronous test handling and the existing test.callback and test.done entry points. Trace how callbacks are currently registered and completed, then check the proposed cases involving multiple callbacks and nested callbacks. Done means the optional callback wrapper preserves the original callback behavior and invokes test.done only after all expected callbacks complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.