question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ember 2.15 - Integration tests with rejected promise

See original GitHub issue

Using 2.14.1, I can test both success and failure scenarios work when resolving promises.

Switching to v2.15.0-beta.1 however results in this error:

screen shot 2017-08-04 at 14 18 49

I was unable to replicate in a Twiddle, so I have created a demo repo instead.

The relevant files are: my-component.js my-component-test.js

Unsure, but might be related to https://github.com/emberjs/ember.js/issues/15490

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (16 by maintainers)

github_iconTop GitHub Comments

4reactions
rwjbluecommented, Nov 28, 2017

OK, I have finally run this issue to ground. It’s a doozy…

The underlying principle in tests is that rejected promises which are not handled within the same run loop trigger the unhandled rejection handler (which by default calls assert.ok(false, ....) and fails the current test). Generally speaking, this is very desireable ergonomics wise.

Given this test (which passes on Ember <= 2.14 and fails on 2.15+):

test('error', function(assert) {
  assert.expect(1);

  this.set('promise', RSVP.reject('bar'));

  this.render(hbs`{{my-component promise=promise}}`);

  return wait().then(() => {
    assert.equal(this.$().text(), 'error: bar');
  });
});

We can see that the following things are happening:

  1. create a rejected promise (RSVP.reject('bar')) which queues up the unhandled rejection assertion
  2. invoke this.set (which is essentially Ember.run(Ember.set, this, rejectedPromise))
  3. invoke this.render (which is ultimately going to instantiate the component with the provided promise, and call promise.then(handleSuccess, handleFailure))

The primary difference between the Ember versions is how the unhandled rejection assertion is delivered.

In Ember <= 2.14 the assertion is queued up to be ran after a flush of the run loop on the next tick (e.g. setTimeout(flushAndAssert, 0)). Since both 2) and 3) above are synchronous, this meant that under Ember <= 2.14, the promise rejection was handled by the time the unhandled rejection assertion is ran, and therefore no assertion is made.

In Ember 2.15+ (versions which include https://github.com/BackburnerJS/backburner.js/pull/203), the assertion is queued up to be ran after the flush of the next run loop. This means that when the run loop from 2) is ran, the assertion is triggered and therefore the test fails.

Unfortunately, I believe the change in behavior is desirable and should remain.


There are a few different ways to successfully test this interaction. I’ve made separate PR’s to the demo repo to show each of them:

  1. Pass in a pending promise, then either resolve or reject as needed: https://github.com/amk221/-ember-rsvp-test-failure/pull/1
  2. Chain off of the rejected promise to reset its “unhandled rejection” state and avoid the assertion: https://github.com/amk221/-ember-rsvp-test-failure/pull/2
  3. Monkey patch Ember.Test.adapter.exception to swallow the unhandled rejection assertion: https://github.com/amk221/-ember-rsvp-test-failure/pull/3
2reactions
rwjbluecommented, Oct 2, 2017

I missed that there was a demo repo in your intro (sorry about that), I’ll try to poke at this later today…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ember Integration Test: Promise rejected before it renders
Suddenly some of my other UNRELATED/UNTOUCHED component integration tests stopped working, returning the following error: Promise rejected ...
Read more >
RSVP.Promise - 2.15 - Ember API Documentation
Promise objects represent the eventual result of an asynchronous operation. ... A promise can be in one of three states: pending, fulfilled, or...
Read more >
[Solved]-How do I create a promise in Ember.js for an Ember ...
Basically you can create a promise like this: var promise = new Ember.RSVP.Promise(function(resolve, reject){ // succeed resolve(value); // or reject ...
Read more >
Javascript (Browser & Node.js) - Rollbar Docs
Rollbar.js 2.15.0 and later ... Rollbar.js earlier than 2.15.0 ... Rollbar.js supports the ability to catch and report unhandled Promise rejections, ...
Read more >
ember-source | Yarn - Package Manager
Ember.js is a JavaScript framework that greatly reduces the time, effort and resources needed to build any web application. It is focused on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found