简体   繁体   English

业力无法找到angular2“桶”进口

[英]Karma cannot find angular2 “barrel” import

We've got an Angular2 project using the barrel import pattern. 我们有一个使用桶导入模式的Angular2项目。 I'm attempting to write unit tests for it, however Karma cannot seem to find the modules that are referenced by the barrel statements. 我正在尝试为此编写单元测试,但是Karma似乎无法找到bucket语句所引用的模块。

The file being tested imports using: 要测试的文件使用以下命令导入:

import {provider1, provider2, provider3} from "../../../core/providers";
import {component1} from "../../controls";

And karma outputs the following warnings: 业力输出以下警告:

13 05 2016 08:57:43.732:WARN [web-server]: 404: /base/src/app/core/providers
13 05 2016 08:57:43.732:WARN [web-server]: 404: /base/src/app/core/controls

Now the section of the karma config file that loads our client scripts: 现在,karma配置文件的部分加载了我们的客户端脚本:

// paths loaded via module imports
{ pattern: "src/app/**/*.js", included: false, watched: true },
// paths to support debugging with source maps in dev tools
{ pattern: "src/app/**/*.ts", included: false, watched: false },

This should load all the client scripts, but I think that since the file I am unit testing uses a barrel import, Karma doesn't know what to do with it. 应该加载所有客户端脚本,但是我认为由于我正在单元测试的文件使用了桶式导入,因此Karma不知道该如何处理。 Neither do I... 我也不...

I have attempted to add a proxy for this barrel pattern, but that did not work. 我试图为该桶形样式添加代理,但这没有用。

What should I be doing? 我该怎么办?

Turns out that using SystemJS with Angular2, you need to give the SystemJS config file information about what a "barrel" means or TypeScript will have no idea what to look for when it's compiling your project. 事实证明,将SystemJS与Angular2结合使用时,您需要向SystemJS配置文件提供有关“桶”的含义的信息,否则TypeScript将不知道在编译项目时要查找的内容。 Meaning, when you say: 意思是,当您说:

import {component1} from "../../controls";

TypeScript will look for "../../controls.js" by default, unless you tell it not to. 默认情况下,TypeScript会查找“ ../../controls.js”,除非您告知不要这样做。 In my main config file I had specified some mappings for these for SystemJS to know what to look for in these buckets as so: 在我的主配置文件中,我为SystemJS指定了一些映射,以了解在这些存储区中要查找的内容:

packages: {
  "src/path/path/controls": {
    "main": "index"
  },
  "src/path/path/providers": {
    "main": "index"
  }
},

However, I also needed to add this to the karma-test-shim.js file so Karma would know what to look for. 但是,我还需要将其添加到karma-test-shim.js文件中,以便Karma知道要查找的内容。

After adding this to my karma-test-shim.js file, it worked fine. 将其添加到我的karma-test-shim.js文件后,它可以正常工作。

var packages = {
    "base/src/path/path/controls": {
        "main": "index"
    },
    "base/src/path/path/providers": {
        "main": "index"
    }
};
System.config(packages);

is the line ending 这行是结束

...watched: ...观看:

truncated? 截断? missing true/false}, 缺少true / false},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM