Completing Promises after runs/waitsFor in Jasmine 1.3

78 views
Skip to first unread message

Galder Zamarreño

unread,
Dec 19, 2016, 1:47:34 AM12/19/16
to Jasmine
Hi,

I'm using jasmine-node 1.14.5, which underneath uses jasmine 1.3, and I'm having issues getting runs/waitFor to work properly with Promises.

In certain tests, I'd like to runs/waitFor to wait for a particular condition to happen, and when it occurs, fulfil a Promise that I return back. However, the moment I attempt to construct a Promise passing in the function(success, fail) parameter, none of the code inside the runs/waitFor gets called. However, if the Promise is resolved directly, it works. Any idea the former option does not work?

To give some examples, the following works fine:

  it("should support async execution of test preparation and expectations", function(done) {
    var p = Promise.resolve("boo")
      .then(function() {
        var p2 = Promise.resolve("whatever");

        runs(function() {
          flag = false;
          value = 0;
          intId = setInterval(function() {
            console.log(value);
            if (++value == 3) { clearInterval(intId); flag = true; }
          }, 500);
        });

        waitsFor(function() {
          return flag;
        }, "The Value should be incremented", 5000);

        runs(function() {
          expect(value).toEqual(3);
        });

        return p2;
      });
    p.then(function() {
      done();
    }).catch(function(err) {
      done(err);
    });
  });

But on the other hand, this does not work because although runs/waitsFor are called without problems, the callbacks inside do not:

  it("should support async execution of test preparation and expectations", function(done) {
    var p = Promise.resolve("boo")
      .then(function() {
        return new Promise(function (fulfil, reject) {
          runs(function () {
            flag = false;
            value = 0;
            intId = setInterval(function () {
              console.log(value);
              if (++value == 3) {
                clearInterval(intId);
                flag = true;
              }
            }, 500);
          });

          waitsFor(function () {
            return flag;
          }, "The Value should be incremented", 5000);

          runs(function () {
            expect(value).toEqual(3);
            fulfil();
          });
        });
      });
    p.then(function() {
      done();
    }).catch(function(err) {
      done(err);
    });
  });

I've also tried the following in the off chance but does not work either, it behaves the same way as the previous example:

  it("should support async execution of test preparation and expectations", function(done) {
    var p = Promise.resolve("boo")
      .then(function() {
        var outerFulfil;
        var outerReject;
        var p2 = new Promise(function(fulfil, reject) {
          outerFulfil = fulfil;
          outerReject = reject;
        });

        runs(function() {
          flag = false;
          value = 0;
          intId = setInterval(function() {
            console.log(value);
            if (++value == 3) { clearInterval(intId); flag = true; }
          }, 500);
        });

        waitsFor(function() {
          return flag;
        }, "The Value should be incremented", 5000);

        runs(function() {
          expect(value).toEqual(3);
          outerFulfil();
        });

        return p2;
      });
    p.then(function() {
      done();
    }).catch(function(err) {
      done(err);
    });
  });

Any idea how to solve it? Although the first example works, it does not behave the way I want because I only want the promise to be fulfilled once the assertions after the waitsFor have been carried out.

Cheers, 
Galder

Gregg Van Hove

unread,
Feb 9, 2017, 1:44:01 PM2/9/17
to jasmi...@googlegroups.com
It looks like you're mixing and matching the old Jasmine async syntax (`runs` and `waitsFor`) with the new `done` callback method. I see both things in your specs.

Unfortunately, we haven't been supporting the 1.3 branch of Jasmine for a few years now, so I'm not really sure what could be going wrong. Can you try upgrading to the 2.x release (we're on 2.5 now) and see if the new `done` async functionality works better?

Hope this helps. Thanks for using Jasmine!

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages