1

我最近将 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 语法遇到这些问题,或者可能会看到我做错了什么?另外,我autoboot = false;environment.js文件中设置了没有区别的文件。测试套件还有一两个测试仍然用旧的 ember-qunit 语法编写。任何帮助,将不胜感激!

4

1 回答 1

0

首先,一些背景故事:

我们的应用程序使用第 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!
  ...
}

奇怪的是上面的代码在developmentandproduction环境中工作。这种变化似乎是由于与容器特别相关的内部 Ember API 被弃用/重新安排的结果。请参阅此页面了解更多信息

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

有关应用程序实例初始化程序的更多信息,请参见此处

相反,代码被移到了应用程序路由的beforeModel()钩子上。我还为度量服务添加了一个实例初始化程序,从而加快了应用程序的加载时间。

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

于 2019-10-04T22:55:56.320 回答