Feature request: Automatic test.done() call in asynchronous tests
- Langage dominant
- JavaScript
- Étoiles
- 1.9k
- Forks
- 359
- Métriques de merge des PR
- Aucune PR mergée en 30 j
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;
};
}
```
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Commencez par localiser la gestion des tests asynchrones et les points d’entrée existants test.callback et test.done. Suivez la manière dont les callbacks sont actuellement enregistrés et terminés, puis vérifiez les cas proposés impliquant plusieurs callbacks et des callbacks imbriqués. C’est terminé lorsque le wrapper de callback facultatif préserve le comportement du callback d’origine et n’appelle test.done qu’une fois tous les callbacks attendus terminés.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript
- Domaine
- testing
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100