I'm trying to write a Javascript testing framework from scratch and I'm trying now to tackle asynchronous testing. Specifically, I'm trying to figure out how the Jasmine 'done' paradigm works. The code I have now is:
const asyncEnsure = function(title, block, assertion) {
let done = false;
let cleared = false;
let complete = function() { done = true; };
block(complete)
let interval = setInterval(function() {
if (done === true) {
cleared = true
clearInterval(interval);
}
}, 1);
let interval2 = setInterval(function() {
if (cleared === true) {
ensure(title, assertion)
clearInterval(interval2)
}
}, 1)
};
And my thinking is that the interval2 which contains the assertion will only trigger once the first block (which contains the entire test apart from the assertion) completes. But this isn't working - all tests are passing...
I've seen the old syntax (runs and waitsFor) which Jasmine uses and I'm guessing this is what the 'done' does behind the scenes but I can't find source code for it.
Can anyone help me write these async tests? I would really appreciate seeing exactly how these work but I've been banging my head against this for ages with no luck.
Thanks!
Aucun commentaire:
Enregistrer un commentaire