【发布时间】:2017-06-15 17:49:12
【问题描述】:
我正在按照官方 angular2 指南对现有项目进行测试。
我使用自定义库downloadjs,它在我运行应用程序时运行良好。但是如果测试运行,我会在控制台中收到错误:
"__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"
我使用npm install downloadjs 获取工具。
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");
}
}
我在同一个文件夹中创建了一个 defs.spec.ts 文件:
declare module 'downloadjs'{
function download(data:any, strFileName:string, strMimeType:string):void;
export = download;
}
并添加了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'
所以 npm start. 运行得很好
但正如指南所述,在创建 1st.spec.ts 后:
describe('1st tests', () => {
it('true is true', () => expect(true).toBe(true));
});
这会引发我粘贴在顶部的错误。感谢您的宝贵时间!
【问题讨论】:
-
如果您使用 npm 手动安装,您是否尝试过全局安装库:npm install -g downloadjs
-
试过了,同样的错误。
标签: javascript node.js unit-testing angular typescript