ember.js: Allowing rejected promises in tests
I’m writing tests for Ember Simple Auth and want to test the case that the authenticator rejects authentication. Therefor I stub its authenticate method to return a rejected promise:
sinon.stub(authenticator, 'authenticate').returns(Ember.RSVP.reject('error'));
The problem now ist that this immediately causes RSVP.onerrorDefault to run which fails the test: Test.adapter.exception(error);.
I don’t see a clean way to disable that behavior so that it allows this specific promise rejection. A working solution I came up with is
var originalPromiseError;
beforeEach(function() {
originalPromiseError = Ember.RSVP.Promise.prototype._onerror;
Ember.RSVP.Promise.prototype._onerror = Ember.K;
sinon.stub(authenticator, 'authenticate').returns(Ember.RSVP.reject('error'));
});
afterEach(function() {
Ember.RSVP.Promise.prototype._onerror = originalPromiseError;
});
but that’s obviously not sth. I want to be doing as it uses private API.
I think it would be great to have some way of using unhandled promise rejections in tests. While that would most likely not be of use for applications, I assume that more addons than just ESA might have a need for sth. like this.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 59 (36 by maintainers)
Commits related to this issue
- add health service tests & refactor refactor health service in order to make it possible to test within Ember's testing assumptions... see here for more details: https://github.com/emberjs/ember.js/i... — committed to Aperta-project/Aperta by deleted user 7 years ago
- add health service tests & refactor refactor health service in order to make it possible to test within Ember's testing assumptions... see here for more details: https://github.com/emberjs/ember.js/i... — committed to Aperta-project/Aperta by deleted user 7 years ago
- add health-service add the health-service to check /health and start it in the app controller the health service had to be written in such a way as to make it possible to test within Ember's testing... — committed to Aperta-project/Aperta by deleted user 7 years ago
- ember does not like rejected promises in tests even if they are intended https://github.com/emberjs/ember.js/issues/11469 — committed to nypublicradio/nypr-ui by deleted user 7 years ago
- Fix promise-related error in edit component test Rejected promises give the qunit tests some issues, maybe as per https://github.com/emberjs/ember.js/issues/11469 We can use the factoryGuy mockUpdat... — committed to Aperta-project/Aperta by Bestra 7 years ago
I’m not here to debate the “right or wrong” of testing exceptions but I did want to offer a workaround for those like me who value testing 500 errors in acceptance tests (whether you are using ember-data or something else that wraps RSVP). Full credit for this goes to my good friend @williamsbdev who found the least hacky/but most functional way to get this working 😃
Here’s what I figured out:
QUnit is designed to fail tests on all errors, including unhandled rejected promises. Mocha won’t do that unless you use an assertion against a rejected promise.
QUnit does not have an API to squelch those errors, making a test pass. There is
assert.throws, but:Ember.onerroris an old and quite poorly documented feature of Ember:ember-mochadoes not seem to use it.ember-qunitdoes use it (see below), but does not document it, keeping us clueless.I had an impression that
Ember.onerroris a candidate for deprecation and should be avoided. @amk221 kindly helped me understand that it has special behavior inember-qunit: catching an error inEmber.onerrorwould prevent QUnit from failing a test!At the same time, QUnit would ensure
Ember.onerroris not catching all errors. In case it does, you should see a failing test case. To avoid this, you need to re-throw errors after catching them inEmber.onerror.@amk221 pointed out that
Ember.onerroronly catches errors that are thrown within the Ember runloop. For some reason, this throw:…would throw the error outside of the runloop, and
Ember.onerrorwill ignore it!The solution is to wrap it with
runfrom'@ember/runloop'. This is the first time I’ve had to userunin app code.Now I have control over errors in tests again, which I lost after
Ember.Test.adapter.exceptionwas removed.Big thanks to @amk221 for taking time and effort to help me figure this out.
@toranb - Thank you for calling attention to this.
I would like to give some background. One of the applications that we were working on had great validations before the request was made so it would not get a
400. Instead of handling all the errors with each ajax request, we wanted to catch all the errors across the application. So we setup something like this:The problem then came when we wanted to test. This is why the monkey patching (done above in the comment by @toranb) on
Ember.Logger.errorandEmber.Test.adapter.exceptionwere done. I was not terribly fond of what I did but it gave me some confidence that the application would respond in the desired manner.I completely understand what @stefanpenner was saying about handling the error by the end of the run loop. When running the tests, you don’t know if the error has been handled and the test framework is going to let the user know that something is broken. This is great. We just took advantage of the
Ember.RSVP.on("error"which allowed it to handle the error and had to trick the test into allowing it.I do this:
I can see the first
debuggerstatement firing, but the second one never fires, and tests are red.Anyway, I believe,
Ember.onerroris (was?) a hook that lets you log your errors to services like Sentry/Bugsnag. It’s not a way to intercept errors.We use Ember Data, currently at version
3.13.1. No jQuery.I’m a bit at a loss here. The previous solution of using
Ember.Test.adapter.exceptionhas been removed from Ember, and there is no replacement.I think this post from EmberMap could be very helpful for people that end up in this conversation:
https://embermap.com/notes/86-testing-mirage-errors-in-your-routes
Then why is the following code breaking?
I get to the
catch, but the test still fails. Is it becausefailToDoXdoes its own run-loop wrapping?This I can get on board with 😃
@stefanpenner
Seems perfectly normal to me. How else would I test that I show an appropriate error message when I get a 422 response from some API?
Also: this behavior is not just bad, it’s also inconsistent.
This doesn’t break the test:
But this does:
because in the first case
Test.adapter.exception(error);isTest.adapter.exception(undefined);and the test adapter ignores it.