ember-test-helpers icon indicating copy to clipboard operation
ember-test-helpers copied to clipboard

Test not waiting promises

Open raphaelns-developer opened this issue 7 years ago • 5 comments

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.

raphaelns-developer avatar Nov 15 '18 12:11 raphaelns-developer

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

rwjblue avatar Nov 21 '18 16:11 rwjblue

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)?

raphaelns-developer avatar Nov 22 '18 01:11 raphaelns-developer

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?

viniciussbs avatar Dec 07 '18 14:12 viniciussbs

Maybe related to https://github.com/emberjs/ember-test-helpers/issues/347?

viniciussbs avatar Dec 07 '18 14:12 viniciussbs

ember-fetch creates a test waiter that should be waiting for the fetch before continuing.

rwjblue avatar Dec 07 '18 15:12 rwjblue