Waiting for unfulfilled promises - ember-testing-wait-hooks feature


#1

I’m encountering a problem where my tests don’t wait for promises to fulfill before continuing. @stefan pointed me towards ember-testing-wait-hooks feature thats in the Canary release.

I build a prototype of a promise tracker that allows me to wait for my promises to fulfill before continuing with the execution of the tests.

Code - Run App - Run Tests

Here is some key code from the example:

var PromiseTracker = Ember.ArrayProxy.extend({
  newPromise: function(callback) {
    // save a reference to it in the tracker and return a new promise
    var promise = Ember.RSVP.Promise(callback);
    this.pushObject(promise);
    return promise;
  },
  areFulfilled: function(){
    // return true if all promises are fulfilled
    return this.everyBy('isFulfilled');
  }
});

// In real scenario, this would probably be injected into your App
App.PromiseTracker = PromiseTracker.create({
  content: []
});

// waiter helper
Ember.Test.registerWaiter(function() {
   return App.PromiseTracker.areFulfilled() === true;
});

Example test

  asyncTest("Link-to with a promise using run.timeout in model", function(){
    expect(2);
    start();
    visit("/");
    click(".red-timeout");
    wait().then(function(){
      equal(find('h3').text(), 'Item');
      equal(find('h4').text(), 'Color: red');
    });
  });

I’m going to write actual code into the ember-pouchdb test suite now.

Would love feedback.


#2

So then I assume you have to explicitly register the promises in your app by calling the newPromise method?