Why "waits" method is flagged as obsolete?

472 views
Skip to first unread message

Gian Marco Gherardi

unread,
Jan 12, 2012, 12:52:30 PM1/12/12
to jasmi...@googlegroups.com
/**
 * Waits a fixed time period before moving to the next block.
 *
 * @deprecated Use waitsFor() instead
 * @param {Number} timeout milliseconds to wait
 */


How to implement waits behavior with waitsFor?

Doug Reeder

unread,
Jan 13, 2012, 10:55:08 AM1/13/12
to jasmi...@googlegroups.com
On Jan 12, 2012, at 12:52 PM, Gian Marco Gherardi wrote:

>
> How to implement waits behavior with waitsFor?

waitsFor(function () {return jig.httpResponse;}, "getting status ", 35000);

pass a function that returns a truthy value when Jasmine can stop waiting. In this case, when a response has been received from an HTTP server.

Doug Reeder
reed...@gmail.com
http://reeder29.livejournal.com/
https://twitter.com/reeder29

https://twitter.com/hominidsoftware
http://outlinetracker.com


Gian Marco Gherardi

unread,
Jan 14, 2012, 8:33:13 AM1/14/12
to jasmi...@googlegroups.com
In my scenario, i just have to wait for an amount of time (because i'm testing that a function is called after a while) so, for me, waits function is the perfect fit. I just don't understand why this function is considered deprecated, given that the alternative implementations feels more like a workaround.

Rajan Agaskar

unread,
Jan 14, 2012, 1:10:54 PM1/14/12
to jasmi...@googlegroups.com
We've deprecated it to encourage people to use latch functions. Using arbitrary waits will slow down your test suite unnecessarily. You can still use an arbitrary wait if you like, simply pass an empty function and message to waitsFor. IE, to do a waits(500), simply:

waitsFor(function () {}, "Message when you timeout", 500);

I actually would suggest avoiding any kind of wait in your test suite if at all possible; often it indicates that something is wrong with the organization of your code. However, I realize some people can't avoid it, and even further, sometimes you just need the regular waits behavior. We won't un-deprecate waits, but feel free to redefine waits so that it calls through to waitsFor with an empty function as I've illustrated above.

Thanks for using Jasmine!

Rajan

On Sat, Jan 14, 2012 at 5:33 AM, Gian Marco Gherardi <gianmarco...@gmail.com> wrote:
In my scenario, i just have to wait for an amount of time (because i'm testing that a function is called after a while) so, for me, waits function is the perfect fit. I just don't understand why this function is considered deprecated, given that the alternative implementations feels more like a workaround.

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/P8DEPiRhkYYJ.

To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.

Gian Marco Gherardi

unread,
Jan 14, 2012, 3:19:41 PM1/14/12
to jasmi...@googlegroups.com
I have to admit that the scenario that i'm testing is a bit unconventional, however in this scenario waits feels very natural.

In general, i agree that "waits" can lead to misuse and so make tests unnecessary fragile and slow.

Thanks all for taking time to respond to my questions! And thanks for this fantastic framework!

Scott B

unread,
Jan 15, 2012, 11:41:26 AM1/15/12
to Jasmine
Remember that Jasmine is very extensible. For example I have added
asyncSpecWait() and asyncSpecDone() like was done with jasimine-node.
I find it to be a better syntax in some situations.

If you want to have a timed wait if Jasmine eliminates it (although I
agree this should be almost useless), just write your own in a helper.

function timedWait(time) {
var done = false;
setTimeout(function() {done = true},time);
waitsFor(function() { return done });
}

GuyE

unread,
Jan 13, 2012, 10:47:54 AM1/13/12
to Jasmine
From: https://github.com/pivotal/jasmine/wiki/Asynchronous-specs

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. This may
mean waiting an arbitrary period of time, or you may specify a
maxiumum period in milliseconds before timing out.

waitsFor(function() {
return spreadsheet.calculationIsComplete();
}, "Spreadsheet calculation never completed", 10000);

In the first argument function you can write any function that will
return true only when you want to proceed with your test run. It can
for example check that an array has values from a different ajax call
or other async process.

On Jan 12, 7:52 pm, Gian Marco Gherardi <gianmarco.ghera...@gmail.com>
wrote:

Kay Plößer

unread,
Feb 17, 2012, 4:59:22 AM2/17/12
to jasmi...@googlegroups.com
But how often does waitsFor() poll?

I have some websocket scripts and I want to test if the scripts get a server-connection.

So I have to write something like this?

var oWebSocket = new WebSocket(....);

waitsFor( function() {
  return ( oWebSocket->readyState === 1 ) true : false; 
}, 'Wating for connection.', 100);

expect(oWebSocket->readyState).toBe(1);

Doug Reeder

unread,
Feb 17, 2012, 1:06:32 PM2/17/12
to jasmi...@googlegroups.com
You probably don't want to have your waitsFor time out in 100 ms.   You can leave out that parameter most of the time.



-- Sent from my Palm Pre 2


--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/cBp-nI0SEvEJ.
Reply all
Reply to author
Forward
0 new messages