Async/await in model hooks fail in tests
See original GitHub issueGiven a model hook that uses async/await and calls Ember.set after an await statement, this will fail in an acceptance test visiting that route, with an You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run error. This does not happen when rewriting this using Promises.
If I understand this correctly, the root issue seems to be that native Promises do not integrate into the Ember run loop, while RSVP.Promise do, and is the same as reported in #15240 and https://github.com/emberjs/rfcs/issues/175. But I thought creating this issue here would help others getting bitten by this to find this.
As reported elsewhere, a workaround is possible by
- replacing the native Promise in tests with the RSVP one: 
window.Promise = Ember.RSVP.Promise;. (you can add that e.g. totests/index.html) - making sure your 
targets.jsis configured to make Babel transpile async/await. Using native async/await in e.g. Chrome does always lead to those failing tests. 
Issue Analytics
- State:
 - Created 6 years ago
 - Reactions:5
 - Comments:10 (6 by maintainers)
 
Top Results From Across the Web
For async tests and hooks, ensure "done()" is called
My test code is, I have the done call back but still getting the error to call the done(); it('remove existing subdocument', (done)...
Read more >Testing Asynchronous Code - Jest
If the promise is rejected, the test will fail. For example, let's say that fetchData returns a promise that is supposed to resolve...
Read more >Ember test with async await functions not working as intended
I am using ember-simple-auth with custom authenticator, which is pretty basic. Just sends login request to backend API, catching error when ...
Read more >Testing asynchronous code - Japa tests runner
The async/await code reads linearly and is easy to reason. Let us share some of the ways you can use to convert callbacks,...
Read more >Async waits in React Testing Library - Reflect.run
Handling when the API call returns with an error or timeout via Promise chaining or a try/catch block and async/await.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@simonihmig @jrjohnson @caseywatts @offirgolan @alvincrespo Perhaps this post sheds some light on the considerations for running with async/await …
http://rwjblue.com/2017/10/30/async-await-configuration-adventure/
Hoping to kickstart more efforts to enable this, but we should probably track the actual issue over on https://github.com/emberjs/rfcs/issues/175 (which I think predates this issue a bit). I’ll try to chime in over there with a quick status update and lay out the game plan that @krisselden is coaching me on…