Ember Unit Test and handling a promise

I have a Utility function that loads some configuration and returns a promise.

export default function() {
  <do something>
  return Ember.RSVP.hash {
      a = Promise1,
      b = Promise2
  };
}

I am creating a test for this in Ember such that I always load this config in beforeEach:

module('Unit:testConfig', {
  beforeEach: function () {
    App = startApp();
    config = fetchConfig();
  }

However, the promise does not resolve before rest of the test starts. I have tried solutions from several threads but don’t understand the right way to do this.

How do you load a random promise, and wait for it to complete before proceding in a test?

1 Like

This is a bit hack, but should work.

module('Unit:testConfig', {
  beforeEach: function () {
    App = startApp();
    QUnit.stop();
    config = fetchConfig().then(function() {
      QUnit.start();
    });
  }

I hope that one of the more experienced Ember folks will chime in and show a better way, but this should get you moving for now.

The simplish explanation for why it happens is that QUnit (for Ember) only really respects promises in the test calls. For example

test('thing with promise', function (assert) {
   return thing.doSomeStuff().then(function() {
      assert.ok(true);
   })
});

returning thing under test causes QUnit Ember to wait for the promise to resolve / reject, in this case assert.ok would only run if the promise resolves, otherwise would raise a ‘no expectation’ error.

Hope this helps :smile: