ember-test-helpers
ember-test-helpers copied to clipboard
Test not waiting promises
Problem
The test is not waiting the promise to return the data. Ember version: 3.4
Test scenario
I'm using triggerKeyEvent to trigger an action at a text input:
await fillIn('[data-test-search-input]', 'Test');
await triggerKeyEvent('[data-test-search-input]', 'keydown', 13);
await settled();
The action goes on a server and gets some data:
actions: {
onTriggerKey() {
return this.getDataFromServer().then(data => set(this, 'data', data));
}
}
But i'm facing some strange behaviours. Sometimes the await settled() wait the promise return the data and sometimes not. This is very hard to reproduce because the test pass in the most of the times.
I'm not sure what getDataFromServer actually does here, but it's likely (from the symptoms that you are having) that the test is not waiting for the async done there.
One thing to be super clear on which seems to be misunderstood is that we do not wait on all promises when running tests, unless something in getDataFromServer is doing one of the things we wait on (scheduled timers, jQuery AJAX requests, and test waiters) the test will not wait for it when you call await settled().
See the docs for settled and getSettledState for details:
https://github.com/emberjs/ember-test-helpers/blob/master/API.md#settled https://github.com/emberjs/ember-test-helpers/blob/master/API.md#getsettledstate
Ok. So i had misunderstood the settled helper thinking that it would wait all promises finish too, but what i can do to the settled waits the getDataFromServer to finish (i can't change any getDataFromServer internal code)?
Hi, @rwjblue. I work with @raphaelns-developer.
getDataFromServer fires an AJAX request using ember-fetch - actually is using ember-apollo-client that uses ember-fetch. So, does settled wait for fetch's AJAX requests or just the ones from jQuery?
Maybe related to https://github.com/emberjs/ember-test-helpers/issues/347?
ember-fetch creates a test waiter that should be waiting for the fetch before continuing.