emberjs / ember-test-helpers

Test-framework-agnostic helpers for testing Ember.js applications
Apache License 2.0
187 stars 257 forks source link

Await visit helper waits for ember runloop later code #1174

Closed colenso closed 2 years ago

colenso commented 2 years ago

I was trying to get started with acceptance testing in our app and had a case where I was setting a flag after 60 seconds. But for some reason the code await visit('/login') waits for the later code to execute. Here's my sample app: https://github.com/colenso/test-app

It's pretty simple: app/controllers/login.js has the code that sets the flag

import Controller from '@ember/controller';
import { later } from '@ember/runloop';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class LoginController extends Controller {

  @tracked
  isReady = false

  @action
  setIsReady() {
    later(() => {
      this.isReady = true;
    }, 60000)
  }
}

app/templates/login.hbs has the code that calls the function

{{page-title "Login"}}
<div
  {{did-insert this.setIsReady}}
>
  {{log this.isReady}}
  {{#if this.isReady}}
    Page is ready now
  {{/if}}
</div>

{{outlet}}

And tests/acceptance/login-test.js has the test code that I got following the guides for writing acceptance tests https://guides.emberjs.com/release/testing/testing-application/

import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';

module('Acceptance | login', function (hooks) {
  setupApplicationTest(hooks);

  test('visiting /login', async function (assert) {
    await visit('/login');

    assert.equal(currentURL(), '/login');
  });
});

The test times out at 60 seconds because for some weird reason it's waiting for the later code to execute.

colenso commented 2 years ago

@rwjblue Any idea on what this is about? Or how I can get around this?

frykten commented 2 years ago

It (the visit helper) waits for the page to be "settled", if I understand well the API Documentation, which kind of makes sense (you usually wanna wait for the whole page to be loaded and finished). For info on the "settled" keyword, lookithere.

Maybe what you're looking for in this particular issue is more like a waitFor(HeaderSelector) (cf. link).

colenso commented 2 years ago

Thanks @frykten this is exactly the behavior that I was experiencing. Since this is not a bug, I'm going to close this issue now.