【问题标题】:setApplication breaks ember-qunit test contextsetApplication 中断了 ember-qunit 测试上下文
【发布时间】:2020-02-01 09:07:31
【问题描述】:

我最近将 Ember 应用程序从 2.18 升级到了 3.13,运行顺利。今天我第一次尝试添加验收测试(在此之前只有集成/单元测试),但测试在第一行失败:

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

module("Acceptance | some route", function(hooks) {
  setupApplicationTest(hooks);

  test("visiting /some-route", async function(assert) {
    await visit("/some-route"); // <----- error thrown here

    assert.equal(currentURL(), "/some-route");
  });
});

我看到了几个错误(按此顺序):

Source:     
TypeError: Cannot read property 'lookup' of undefined
    at Object.initialize (http://localhost:4200/assets/my-app.js:10312:28)
    at http://localhost:4200/assets/vendor.js:61627:21
    at Vertices.each (http://localhost:4200/assets/vendor.js:80243:9)
    at Vertices.walk (http://localhost:4200/assets/vendor.js:80157:12)
    at DAG.each (http://localhost:4200/assets/vendor.js:80087:22)
    at DAG.topsort (http://localhost:4200/assets/vendor.js:80095:12)
    at Class._runInitializer (http://localhost:4200/assets/vendor.js:61653:13)
    at Class.runInitializers (http://localhost:4200/assets/vendor.js:61625:12)
    at Class._bootSync (http://localhost:4200/assets/vendor.js:59923:14)
    at Class.boot (http://localhost:4200/assets/vendor.js:59890:14)


Source:     
Error: Cannot call `visit` without having first called `setupApplicationContext`.
    at visit (http://localhost:4200/assets/test-support.js:44177:13)
    at Object._callee$ (http://localhost:4200/assets/tests.js:23:47)
    at tryCatch (http://localhost:4200/assets/vendor.js:12365:40)
    at Generator.invoke [as _invoke] (http://localhost:4200/assets/vendor.js:12591:22)
    at Generator.prototype.<computed> [as next] (http://localhost:4200/assets/vendor.js:12417:21)
    at asyncGeneratorStep (http://localhost:4200/assets/tests.js:6:105)
    at _next (http://localhost:4200/assets/tests.js:8:196)
    at http://localhost:4200/assets/tests.js:8:366
    at new Promise (<anonymous>)
    at Object.<anonymous> (http://localhost:4200/assets/tests.js:8:99)

经过一番挖掘,似乎有些东西错误地设置了测试上下文。它只是一个空对象:

因此,isApplicationTestContext(context) 返回 false 并引发第二个错误。我猜第一个错误被抛出是因为应用程序有一些执行查找的初始化程序。

为了添加此验收测试,我还将test-helper.js 文件更新为以下内容:

import Application from "../app";
import config from "../config/environment";
import { setApplication } from "@ember/test-helpers";
import { start } from "ember-qunit";

setApplication(Application.create(config.APP));

start();

对于上述文件,所有测试都失败了,所以setApplication 似乎导致测试上下文设置不正确?旧的test-helper.js 文件是这样的:

import resolver from "./helpers/resolver";
import { setResolver } from "@ember/test-helpers";
import { start } from "ember-cli-qunit";

setResolver(resolver);
start();

我尝试重新添加setResolver 调用,但没有任何区别。有没有其他人使用新的 ember-qunit 语法遇到这些问题,或者可能会看到我做错了什么?另外,我在environment.js 文件中设置了autoboot = false;,这并没有什么不同。测试套件还有一两个测试仍然用旧的 ember-qunit 语法编写。任何帮助将不胜感激!

【问题讨论】:

  • 我的猜测是你在从 2.18 升级到 3.13 的过程中忘记了一些东西。测试上下文应在您的验收测试中由setupApplicationTest(hooks); 设置。见github.com/emberjs/ember-qunit/blob/v4.5.1/addon-test-support/…。你用的是什么版本的ember-qunit
  • 我会根据默认蓝图仔细检查您的应用程序。你可以在ember-cli/ember-new-output repo 上找到它。我会特别仔细检查您的config/environment.js、依赖版本和tests/ 文件夹中的所有文件。我将首先验证APP.autoboot 在测试环境中是falsegithub.com/ember-cli/ember-new-output/blob/…
  • @JulienPalmas 我正在使用ember-qunit 版本4.5.1。在您链接的行处暂停执行时 this 只是一个 POJO,所以我猜有些东西事先设置不正确?
  • @jelhan 我仔细检查了默认蓝图,它看起来确实一样。 environment.js 文件似乎没有什么特别之处,我确实将 APP.autoboot 设置为 false 用于测试环境。

标签: ember.js qunit ember-qunit ember-octane


【解决方案1】:

首先,一些背景故事:

我们的应用程序使用第 3 方库作为指标,使用另一个第 3 方库作为特征标志。每个库都有自己的服务,但我们需要在初始化功能标志服务之前初始化指标服务,因为我们希望链接分析用户数据以获得每个用户的正确功能标志。功能标志检查在整个应用程序中完成,因此在进行功能标志检查和分析文件加载到网页上的脚本标记之间会出现竞争条件。

现在解决办法:

出现这个错误的原因:

Source:     
TypeError: Cannot read property 'lookup' of undefined
    at Object.initialize (http://localhost:4200/assets/my-app.js:10312:28)
    at http://localhost:4200/assets/vendor.js:61627:21
    at Vertices.each (http://localhost:4200/assets/vendor.js:80243:9)
    at Vertices.walk (http://localhost:4200/assets/vendor.js:80157:12)
    at DAG.each (http://localhost:4200/assets/vendor.js:80087:22)
    at DAG.topsort (http://localhost:4200/assets/vendor.js:80095:12)
    at Class._runInitializer (http://localhost:4200/assets/vendor.js:61653:13)
    at Class.runInitializers (http://localhost:4200/assets/vendor.js:61625:12)
    at Class._bootSync (http://localhost:4200/assets/vendor.js:59923:14)
    at Class.boot (http://localhost:4200/assets/vendor.js:59890:14)

是因为应用程序有一个应用程序初始化程序,它利用私有 Ember API 从容器中执行查找。初始化程序正在执行查找以在检索分析数据之前隐式初始化指标服务,然后初始化特征标志服务。

export function initialize(application) {
  const container = application.__container__; // <-- undefined
  const lookup = container.lookup.bind(application.__container__); // <-- error!
  ...
}

奇怪的是上面的代码可以在developmentproduction 环境中工作。这种变化似乎是由于与容器特别相关的内部 Ember API 被弃用/重新安排的结果。 See this page for more information.

为了在应用初始化程序中访问容器,必须从应用程序实例(即实例初始化程序)完成。这可能是一个可以接受的解决方案,但我不能 100% 确定我们是否可以推迟应用程序实例初始化程序的应用程序准备就绪。

有关应用程序实例初始化程序的更多信息,see here

而是将代码移至应用程序路由的beforeModel() 挂钩。我还为指标服务添加了一个实例初始化程序,从而加快了应用程序的加载时间。

将初始化程序代码移至应用程序路径后,应用程序现在已成功构建,并且验收测试工作起来就像一个魅力。 :)

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多