Any unhandled error causes acceptance tests to fail, making it impossible to test error states
See original GitHub issueHi!
In our app, we catch errors in order to display flash messages to the user, letting them be aware of an error and offering a chance to retry.
We choose to re-throw the error after displaying the flash message. This is because we want the error to be caught by an error tracking service such as Sentry, Bugsnag, TrackJS, Raygun, etc.
The problem is that QUnit tests would crash on any uncaught error, making it impossible to test error reporting.
There are two userland workarounds from this issue:
- Choose not to re-throw an error. I don’t like this approach because it would require adding manual error tracking commands into every catch block.
- Re-throwing conditionally, only in non-test environment. This is more or less OK, but I don’t feel comfortable adding exceptions for tests. I believe, it’s a slippery slope.
Previously, it was possible to override Ember.Test.adapter.exception
and prevent tests from failing on uncaught assertions. The best part is that you could check for a condition (e. g. a certain error code or message) and re-throw an error if it was not the one you were expecting.
But according to ember-qunit source, it is no longer possible in modern Ember:
// Ember 2.17 and higher do not require the test adapter to have an `exception`
// method When `exception` is not present, the unhandled rejection is
// automatically re-thrown and will therefore hit QUnit's own global error
// handler (therefore appropriately causing test failure)
How do I prevent QUnit from crashing on unhandled errors? What is the modern replacement for overriding Ember.Test.adapter.exception
?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:22 (6 by maintainers)
Top GitHub Comments
@lolmaus - I’ll try to reply to the various sections independently (as there are a few separate points you are making).
FWIW, I think this is the correct stance for QUnit to take (and IIRC I implemented it waaaaaaay back in 2017 https://github.com/qunitjs/qunit/pull/1241). Unhandled exceptions (and unhandled promise rejections) are bad, and in most circumstances point to an actual problem in the application.
I totally agree with you here! We absolutely should document how to handle situations like @amk221’s.
I agree that it is unclear how Ember’s internals handle unhandled rejections. I think we should update the documentation for all async route hooks to explicitly say what happens when an exception occurs. For example, if an exception occurs during
model
and you have anerror
substate orerror
action that rejection is automatically handled for you (this is why you don’t commonly see the problem you are reporting for that category of code). I also think that doing that documentation will significantly help folks think about how exceptions / rejections are handled by the system (and therefore how they should be handled by their own code).To reiterate, I agree with you. We should increase both API and guide documentation for exceptions/rejections and how application developers should handle those situations. FWIW, I do not think the answer is “never leave them uncaught” (as sometimes you really do intend for an error / rejection to bubble all the way up loudly).
I am totally on board with the idea that we should identify specific use case / scenarios, and solve for them. That will help us know what to document (and where). Unfortunately, the example doesn’t have enough information. What is triggering this action? What does the test look like? What kind of test? etc.
Ultimately, I think we should (mostly in order):
I’ve started a PR to finally fix this: ember-test-helpers#1194. I would appreciate your thoughts!