简体   繁体   中英

Angular 2 test cannot load custom package

I am following the offical angular2 guide for testing on an existing project.

I use a custom libary, downloadjs which works just fine when I run the application. 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.

file-handler.utility.js is the following:

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:

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

        export = download;
}

and added the path to 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.

But as the guide states, after creating 1st.spec.ts with this:

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. We decided to use an existing angular 2 template, and after integrating with it, the tests works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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