Feature request: Automatic test.done() call in asynchronous tests
- Lingua principale
- JavaScript
- Stelle
- 1.9k
- Fork
- 359
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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;
};
}
```
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia individuando la gestione dei test asincroni e i punti di ingresso esistenti test.callback e test.done. Traccia il modo in cui i callback vengono attualmente registrati e completati, quindi verifica i casi proposti che coinvolgono più callback e callback annidati. Il lavoro è completo quando il wrapper di callback opzionale preserva il comportamento originale del callback e invoca test.done solo dopo il completamento di tutti i callback previsti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript
- Ambito
- testing
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100