我正在遵循官方的 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));
});
这会引发我粘贴在顶部的错误。感谢您的宝贵时间!
最佳答案
该问题与 defs.spec.ts 位置有关。我们决定使用现有的 Angular 2 模板,与它集成后,测试现在可以工作了。
关于javascript - Angular 2测试无法加载自定义包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938146/