Monday, June 3, 2024
99
rated 0 times [  103] [ 4]  / answers: 1 / hits: 27108  / 11 Years ago, mon, may 6, 2013, 12:00:00

I use jasmine runs and wait to test asynchronous operations. Everything works fine but I'm not quite sure what goes on behind the scenes.



The jasmine documentation states the following example to which I added three log statement.



describe(Asynchronous specs, function() {
var value, flag;

it(should support async execution of test preparation and exepectations, function() {

runs(function() {
flag = false;
value = 0;

setTimeout(function() {
flag = true;
}, 500);
});

waitsFor(function() {
value++;
if(flag) {
console.log(A);
}
return flag;
}, The Value should be incremented, 750);

console.log(B);

runs(function() {
console.log(C);
expect(value).toBeGreaterThan(0);
});
});
});


});



The first runs and waitsFor are perfectly clear to me. Runs starts an asynchronous operation and waitsFor waits for a condition.



However I do not understand why the second runs does not start until the waitsFor is finished. The waitsFor is not a blocking call.



My guess is that waitsFor implicitly blocks any following runs call until it is finished. Is this so?



My evidence is that the console.log statements output:



B A C



But if waitsFor would really block it should be



A B C


More From » unit-testing

 Answers
7

waitsFor does block until the conditions it's waiting for are met or it times out.



From the jasmine docs: waitsFor() provides a better interface for pausing your spec until some other work has completed. Jasmine will wait until the provided function returns true before continuing with the next block..



The linked docs also have a slightly clearer example or waitsFor.



EDIT: Ah I see what you mean now. waitsFor won't block JS that isn't wrapped in runs, waitsFor, ect.



What jasmine is doing is taking the function passed to it via runs or waitsFor and if jasmine is not currently waiting, it executes the function immediately. If it is waiting, it doesn't call it until it's finished waiting.



That doesn't stop the console.log as it's been passed to jasmine so jasmine can't prevent it from being executed straight away.


[#78406] Saturday, May 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alyssiat

Total Points: 608
Total Questions: 102
Total Answers: 101

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
alyssiat questions
;