ember.js: Ember 2.15 - Integration tests with rejected promise
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:
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
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 18 (16 by maintainers)
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+):
We can see that the following things are happening:
RSVP.reject('bar')) which queues up the unhandled rejection assertionthis.set(which is essentiallyEmber.run(Ember.set, this, rejectedPromise))this.render(which is ultimately going to instantiate the component with the provided promise, and callpromise.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 both2)and3)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:
Ember.Test.adapter.exceptionto swallow the unhandled rejection assertion: https://github.com/amk221/-ember-rsvp-test-failure/pull/3I missed that there was a demo repo in your intro (sorry about that), I’ll try to poke at this later today…
We too experienced a version of this in our tests. We use sinon.js for stubbing functions calls.
So a test like below, which was passing before 2.15,
It fails in latest ember (tried in 2.18), where the
RSVP.rejectpropagates up the chain and breaks the test.Apart from @rwjblue suggestion above, changing the line 2 to use sinon
rejectshelper also works. Sinonrejects/resolvesmethods return native JS promise and not RSVP.I believe this will be addressed by https://github.com/BackburnerJS/backburner.js/pull/277 but need to test the reproduction to confirm.