Because your callback will get called more than once, I'd suggest explicitly resolving the deferred rather than wrapping the callback with dfd.callback
. Try something like this:
var dfd = this.async(5000);
conn.connection.connect('jid', 'pass', dfd.rejectOnError(function (status) {
if (status !== 5) {
return;
}
// do any assertions here
// explicitly resolve the test since it was successful to this point
dfd.resolve();
}));
First, you'll notice we wrap the callback with dfd.rejectOnError
. This convenience method will reject the deferred if any Error is thrown during any execution of your callback function. Next, we got rid of the dfd.callback
, as this will attempt to resolve the deferred if the wrapped callback executes without throwing an Error. Because your callback will be called multiple times, this won't work. By explicitly resolving the deferred only when your criteria is met (status !== 5
and any assertions pass) your callback can execute any number of times as long as the deferred resolves within five seconds.