Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does beforeEach support more than 1 promise resolve? #165

Closed
toranb opened this issue Apr 23, 2015 · 4 comments
Closed

Does beforeEach support more than 1 promise resolve? #165

toranb opened this issue Apr 23, 2015 · 4 comments

Comments

@toranb
Copy link

toranb commented Apr 23, 2015

I have a single test that does a beforeEach. Inside this beforeEach I originally had a single promise that I needed to resolve before any tests run.

return new Ember.RSVP.Promise(function (resolve) {
    resolve(true);
});

That worked great - but later when I tried to resolve 2 using RSVP.all I got an error saying

Error: assertion outside test context

Here is the new return statement that throws the above error

module('Acceptance: Blah', {
  beforeEach: function() {
    application = startApp();
    return Ember.RSVP.all([
      new Ember.RSVP.Promise(function (resolve) {
        resolve(true);
      })
    ]);
  },
  afterEach: function() {
    Ember.run(application, 'destroy');
  }
});

test('visiting /blah', function(assert) {
    visit("/");
    andThen(function () {
      assert.equal(1, 1);
    });
});

Here is an example ember-cli project that shows it in action

https://github.com/jcttrll/ember-rsvp-all-issue

Here is a full stack showing what blew up if this helps in any way

Error: assertion outside test context, in     at http://localhost:7357/assets/test-support.js:3625
    at http://localhost:7357/assets/test-support.js:3650
    at http://localhost:7357/assets/test-support.js:4287
    at http://localhost:7357/assets/vendor.js:44312
    at onerrorDefault (http://localhost:7357/assets/vendor.js:36678)
    at http://localhost:7357/assets/vendor.js:57024
    at http://localhost:7357/assets/vendor.js:58036
    at publishRejection (http://localhost:7357/assets/vendor.js:56267)
    at http://localhost:7357/assets/vendor.js:36647
    at http://localhost:7357/assets/vendor.js:10279
    at http://localhost:7357/assets/vendor.js:10344
    at http://localhost:7357/assets/vendor.js:10149
    at http://localhost:7357/assets/vendor.js:9574
    at http://localhost:7357/assets/vendor.js:9629
    at run (http://localhost:7357/assets/vendor.js:25333)
    at http://localhost:7357/assets/vendor.js:44534 at http://localhost:7357/assets/test-support.js, line 3625
@toranb
Copy link
Author

toranb commented Apr 28, 2015

We still can't use RSVP.all but a workaround that solves the issue

module("Acceptance: Some Things Need Loaded Before Tests Start", {
    beforeEach: function () {
        application = startApp();
        return promiseAwaitingThingsReady(thingOne, thingTwo);
    },
    afterEach: function() {
        Ember.run(application, "destroy");
        return promiseAwaitingThingsDestroyed();
    }
});

The functions shown above might look something like this....

var promiseAwaitingThingsReady = function() {
    var thingIds = arguments;
    var ready = [];
    return new Ember.RSVP.Promise(function(resolve) {
        GLOBALTHING.on("instanceReady", function(event) {
            ready.push(event.thing.name);
            for (var i = 0; i < thingIds.length; i++) {
                var thingId = thingIds[i];
                if (ready.indexOf(item) < 0)
                    return;
                }
            }
            event.removeListener();
            resolve(true);
        });
    });
};

var promiseAwaitingThingsDestroyed = function() {
    return new Ember.RSVP.Promise(function(resolve) {
        if (!Object.keys(GLOBALTHING.instances).length) {
            resolve(true);
            return;
        }
        GLOBALTHING.on("reset", function(event) {
            event.removeListener();
            resolve(true);
        });
    });
};

@artsyca
Copy link

artsyca commented May 27, 2015

This may sound crazy, but can you return a promise like:

new Ember.RSVP.Promise(function (resolve) {
    Ember.RSVP.all(...).then(function(){ resolve(true); });
});

@toranb
Copy link
Author

toranb commented May 27, 2015

For better/worse this is what I ended up with

var promiseAwaitingThingsDestroyed = function() {
    return new Ember.RSVP.Promise(function(resolve) {
        if (!Object.keys(SOMEGLOBAL.instances).length) {
            resolve(true);
            return;
        }

        SOMEGLOBAL.on("reset", function(event) {
            event.destoryThing();
            resolve(true);
        });
    });
};

    afterEach: function() {
        Ember.run(application, "destroy");
        return promiseAwaitingThingsDestroyed();
    }

@Turbo87
Copy link
Member

Turbo87 commented Oct 14, 2017

that sound like a very strange issue, because beforeEach() doesn't care what kind of promise is returned.

I haven't noticed any issues like that with any of the more recent releases so I'll go ahead and close this. If this issue is indeed still relevant then please let me know and reopen.

@Turbo87 Turbo87 closed this as completed Oct 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants