【问题标题】:Ember-cli acceptance test times out on await click(); qunit error: Uncaught (in promise) Error: pushFailure()Ember-cli 验收测试在 await click() 上超时; qunit 错误:未捕获(承诺中)错误:pushFailure()
【发布时间】:2019-06-21 20:34:32
【问题描述】:

我正在尝试为 web 应用的登录页面创建验收测试。一切都几乎正常工作,除了 await click(element) 承诺永远不会解决:

import { module, test } from 'qunit';
import { visit, currentURL, fillIn, click, waitFor, getSettledState } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { invalidateSession } from 'ember-simple-auth/test-support';
import { setupMirage } from 'ember-cli-mirage/test-support';

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

  test('login page | login success', async function(assert) {
    assert.timeout(5000);

    console.log('start', getSettledState());

    await visit('/login');
    assert.equal(currentURL(), '/login');

    await waitFor('.btn-primary:disabled');

    await fillIn('input[type="text"]', 'mirage');
    await fillIn('input[type="password"]', 'password1234');

    await waitFor('.btn-primary:not([disabled])', 2000);
    console.log('btn enabled');

    let btnSubmit = document.querySelector('.btn-primary');
    assert.equal(btnSubmit.disabled, false);

    await click(btnSubmit);
    console.log('await btn click');

    await waitFor('.nav-tabs', 4000);
    console.log('nav complete');

    assert.equal(currentURL(), '/login-success');
    console.log('finished', getSettledState());
  });
});

如果我按原样运行此测试,“等待 btn 单击”不会登录控制台,直到超时。我还收到 qunit 错误 "Uncaught (in promise) Error: pushFailure() assertion outside test context, in ___ at internalStart"(下划线由我添加)

但是,如果我删除 click(btnSubmit) 调用的 await 部分,测试成功完成,但是最后一次检查 getSettledState() 会返回:

hasPendingRequests: true
hasPendingTimers: true
hasPendingWaiters: false
hasRunLoop: true
pendingRequestCount: 1

由于有待处理的请求和计时器,测试仍然超时,即使所有 assert() 调用都成功了。

所以看起来如果我使用await click(btnSubmit) 运行测试正确,点击()上的测试超时,但如果我只是调用click(btnSubmit),测试成功完成,虽然testem 或 qunit 并不知道所有已完成的测试。我做错了什么?

海市蜃楼登录端点:

this.post('/login', function(db, request) {
    let formData = JSON.parse(request.requestBody);
    let auth = this.serialize(db.profiles.all());
    if (formData.identification !== auth.data[0].attributes.loginid || formData.password !== auth.data[0].attributes.password) {
      return new Response(401, {some: 'header', 'Content-Type': 'application/json'}, {
        error_code: "err_loginAuthenticationFail"
      });
    }
    let profile = this.serialize(db.profiles.all());
    profile.data[0].attributes.id = profile.data[0].attributes.localid
    delete profile.data[0].attributes.localid;
    return { ...longAccessTokenObject }
});

mirage 端点运行正常,它验证我设置的一个用户/密码组合,无论是在测试中还是在 Chrome 中手动使用 /login 页面。

【问题讨论】:

  • 另外,当我在 Chrome 中查看测试时,一切正常。无论我使用 await click(btnSubmit) 还是仅使用 click(btnSubmit)。登录成功后,我可以看到 Chrome 导航到预期的页面。
  • 您使用的是哪个 ember 版本?另外,您是否在任何地方使用 ember 并发?网络套接字?有while(true)s 吗?您如何模拟身份验证端点? (我看到你正在使用海市蜃楼)。你可以添加更多的代码,或最小的复制?谢谢! :)
  • 另外,当测试驱动点击发生时,您是否在控制台中出现任何错误?我的直觉是有一个请求没有完成,或者没有被海市蜃楼捕获。 (pendingRequestCount: 1)
  • Ember 3.10。使用了ember-concurrency,但不在登录过程中。根本没有 websockets,登录中没有 while() 语句。
  • 发生 click() 事件时不会记录错误。然而,代码执行停止,因为没有进一步的 console.log() 发生。在 click() 事件之前 getSettledState() 返回:hasPendingRequests: false hasPendingTimers: true hasPendingWaiters: false hasRunLoop: true pendingRequestCount: 0 在按钮 click() 之后,有很长的延迟,然后 getSettledState() 返回:hasPendingRequests: false hasPendingTimers: true hasPendingWaiters: false hasRunLoop: true pendingRequestCount: 0 此外,其他对 click(someBtn) 的调用在其他接受中工作正常测试。

标签: javascript ember.js ember-cli qunit testem


【解决方案1】:

NullVoxPopuli 100% 正确。我跟进了你的怀疑,代码库中有一个 ember-concurrency 任务,我不知道它没有解决。

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 2022-12-19
    • 2021-03-03
    • 2021-02-01
    • 1970-01-01
    • 2021-11-28
    • 2021-12-31
    • 2019-11-24
    • 2020-06-03
    相关资源
    最近更新 更多