Skip to content

Instantly share code, notes, and snippets.

@amitzur
Last active December 20, 2015 10:08
Show Gist options
  • Save amitzur/6112906 to your computer and use it in GitHub Desktop.
Save amitzur/6112906 to your computer and use it in GitHub Desktop.
Multiple waitsFor in a jasmine test

Asynchronous Testing with Jasmine

Creating async tests with jasmine is quite intuitive.

If you've read the documentation, then you are aware of the waitsFor and runs helpers in Jasmine. I had more than one async action in my test, so it required an inspection on my side to verify that Jasmine would run the test in the same way I had expected.

If you specify a series of waitsFor or waits and runs callbacks, then Jasmine will start the countdown for each waitsFor at the appropriate time (in this gist - after the previous runs callback was completed).

This gist shows a successful test that performs 2 async increments of x. The values of the countdowns indicate that the callbacks are called at the appropriate time, otherwise the test will fail at the second waitsFor.

describe("Gist for showing an example of multiple waitsFor in a jasmine test", function() {
it("Runs 2 asynchronous increments of 'x' and expects the result of these 2 increments", function() {
var x = 1;
setTimeout(function() {
x = 2;
}, 100);
waitsFor(function() {
return x === 2;
}, 150, "first increment");
runs(function() {
setTimeout(function() { x = 3; }, 100);
});
waitsFor(function() {
return x === 3;
}, 150, "second increment");
runs(function() {
expect(x).toEqual(3);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment