【问题标题】:Test component with "declare var"使用“declare var”测试组件
【发布时间】:2016-12-12 10:51:25
【问题描述】:

在我的 angular2 应用程序中,我有带有 var 的 js 文件“connection.conf.js”:

var appTypeConf = "example-app";

组件使用来自“connection.conf.js”的变量:

declare var appTypeConf: string;

export class Component {
  public appType = appTypeConf;
}

它在我的应用程序中可以正常工作,但是当我使用 karma 测试应用程序时, 我收到一个错误:

ReferenceError: Can't find variable: appTypeConf (line 9)

如何正确声明组件spec文件中的变量?

【问题讨论】:

标签: unit-testing angular typescript karma-jasmine


【解决方案1】:

我找到了解决方案,我必须将 connection.conf.js 路径添加到 karma.conf.js 中的 files 数组

var files = [
  // ***
  'app/connection.conf.js'
]

【讨论】:

  • 这对我也有效,但我必须指出,包含全局变量的文件必须作为第一项放在karma.conf.js 的filesarray 中。
  • 你能把你的代码形式 karma.conf.js 和你写的测试用例粘贴在这里吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
相关资源
最近更新 更多