简体   繁体   English

Angular 2 测试无法加载自定义包

[英]Angular 2 test cannot load custom package

I am following the offical angular2 guide for testing on an existing project.我正在按照官方的 angular2 指南对现有项目进行测试。

I use a custom libary, downloadjs which works just fine when I run the application.我使用自定义库downloadjs ,当我运行应用程序时它工作得很好。 But I get an error in the console in case of test running:但是在测试运行的情况下,我在控制台中收到错误消息:

"__zone_symbol__zoneAwareStack": "Error: (SystemJS) XHR error (404 Not Found) loading node_modules/downloadjs/download.js\n\tError: XHR error (404 Not Found) loading node_modules/downloadjs/download.js\n\tError loading node_modules/downloadjs/download.js as \"downloadjs\" from app/utilities/file-handler.utility.js"

I used npm install downloadjs to get the tool.我使用npm install downloadjs来获取该工具。

file-handler.utility.js is the following: file-handler.utility.js 如下:

import {Injectable} from "@angular/core";
import downloadjs=require("downloadjs");
@Injectable()
export class FileHandler{

  public static download(fileToDownload: any) {
    downloadjs(fileToDownload, "filename.txt" ,"text/plain");
  }   
}

I have created a defs.spec.ts file in the same folder:我在同一个文件夹中创建了一个 defs.spec.ts 文件:

declare module 'downloadjs'{
    function download(data:any, strFileName:string, strMimeType:string):void;

        export = download;
}

and added the path to systemjs.config.js:并添加了 systemjs.config.js 的路径:

 // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'downloadjs': 'npm:downloadjs/download.js'

So this runs just fine with npm start.所以这在npm start.运行得很好npm start.

But as the guide states, after creating 1st.spec.ts with this:但正如指南所述,在用这个创建 1st.spec.ts 之后:

describe('1st tests', () => {
  it('true is true', () => expect(true).toBe(true));
});

this throws the error I pasted on the top.这会引发我粘贴在顶部的错误。 Thank you for your time!感谢您的时间!

The problem was related to defs.spec.ts location.问题与 defs.spec.ts 位置有关。 We decided to use an existing angular 2 template, and after integrating with it, the tests works now.我们决定使用现有的 angular 2 模板,在与它集成后,测试现在可以工作了。

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

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