caolan / caolan/nodeunit

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

オープン
#289 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
1.9k
フォーク
359
PR マージ指標
30日以内にマージされた PR はありません

説明

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

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

まず、非同期テストの処理と、既存のエントリーポイントである test.callback および test.done を特定します。現在コールバックがどのように登録され、完了として扱われているかを追跡し、その後、複数のコールバックとネストされたコールバックを含む提案されたケースを確認します。オプションのコールバックラッパーが元のコールバックの動作を維持し、期待されるすべてのコールバックが完了した後にのみ test.done を呼び出せば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
testing
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。