3

I've used $timeout with a delay of e.g. 600ms in some service code. In the according test I want to assert that the code really does what it is expected to do after the given 600 ms. Is there any way to progress in time with a certain amount of time (similar to jasmine.Clock.tick(600))? I only know about $timeout.flush(), but that fires everything currently in the $timeout-queue.

1 Answer 1

7

In AngularJS version 1.2 you can do $timeout.flush(600), which is analogous to Jasmine's jasmine.Clock.tick(600).

In addition to that Angular 1.2 provides a very handy $timeout.flushNext(msec) method. It differs from the flush(msec) in that that instead of simply putting a clock given time ahead for you to then check if an expected thing happened or not, the flushNext() method does both tasks at once. It puts a clock ahead up until the next deferred event is fired and then verifies that the elapsed time equals the given one.

Link to the documentation

Sign up to request clarification or add additional context in comments.

3 Comments

Good to see that this is added for version 1.2. It doesn't help us though, as we are currently stuck to version 1.0. Nevertheless thanks for pointing out that this feature will be available.
Might be worth noting that this is present on 1.2.0, but gone from 1.2.1 onwards.
AFAICT, the flushNext function seems to be gone, but the flush function is still there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.