Flushing single timeout function with $timeout.flush() in tests

275 views
Skip to first unread message

Per Rovegård

unread,
Jun 25, 2013, 3:08:52 AM6/25/13
to ang...@googlegroups.com
Hi,

I have some code that executes in steps using $timeout without a delay. However, when I do $timeout.flush() in my test, all timeout functions are flushed, including the new ones that are added/scheduled while flushing. This makes it impossible for me to test that there are multiple timeouts, as opposed to there being a single timeout and then everything happening in one go.

The following fiddle illustrates the problem:


After the first flush, I expect the first timeout function to have been executed, but I want to defer executing the second one until I flush again. 

The only workaround I've found so far is to specify a delay for the second timeout function. I chose 4 ms since that is the minimum delay for current browsers anyway. And that brings me to another thing - shouldn't $timeout.flush() assume that if a timeout has been added without delay, it has a delay of 4 ms?

Cheers,
Per

Miguel Ping

unread,
Jun 25, 2013, 6:01:59 AM6/25/13
to ang...@googlegroups.com
I would say it would be more interesting to provide an argument to flush() in order to tell how many deferreds to flush.

Per Rovegård

unread,
Jun 25, 2013, 9:21:38 AM6/25/13
to ang...@googlegroups.com
More interesting than what?

But I agree that it would solve the problem. But I would also like to say "flush all deferreds that exist now, but not the ones added while flushing" - without knowing the exact number right now.
Reply all
Reply to author
Forward
0 new messages