[英]Angular test does not include import from node_modules
我無法在angular 2的測試中通過node_modules解析外部庫。我缺少某些東西嗎?
我有一項從node_modules導入庫的服務:
import {Injectable} from '@angular/core';
import Lib from 'lib';
@Injectable()
export class Service {
stuff() {
return Lib.doStuff();
}
}
當我運行該應用程序時,便定義了Lib並可以使用該庫。
當我在該類上運行測試時,庫未定義。
describe('tests', () => {
let service: Service;
beforeEach(() => service = new Service());
it('test', () => {
service.stuff();
});
});
測試本身有效。 如果我測試一個硬編碼的返回值,它可以工作,因此整體設置似乎很好。
我使用由angular-cli創建的項目骨架。 一些版本:
@angular/cli: 1.0.0
node: 6.10.2
os: linux x64
@angular/common: 4.0.2
@angular/compiler: 4.0.2
@angular/core: 4.0.2
@angular/forms: 4.0.2
@angular/http: 4.0.2
@angular/platform-browser: 4.0.2
@angular/platform-browser-dynamic: 4.0.2
@angular/router: 4.0.2
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.2
我對棱角還很陌生,所以我不知道幕后會發生什么。 我希望所有進口都得到解決。
不是這樣嗎
如果您的lib export default
使用Lib符號,則只能使用import Lib from 'lib'
,如果不需要,則需要使用以下公式之一:
import * as Lib from 'lib'
要么
import {Lib} from 'lib'
在Mozilla開發人員網絡中了解有關導入的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.