简体   繁体   English

角度测试不包括从node_modules导入

[英]Angular test does not include import from node_modules

I cannot resolve an external library via node_modules in a test in angular 2. Am I missing something? 我无法在angular 2的测试中通过node_modules解析外部库。我缺少某些东西吗?

I have a service that imports a library from node_modules: 我有一项从node_modules导入库的服务:

import {Injectable} from '@angular/core';
import Lib from 'lib';
@Injectable()
export class Service { 
  stuff() {
    return Lib.doStuff();
  }
}

When I run the application, Lib is defined and I can use the library. 当我运行该应用程序时,便定义了Lib并可以使用该库。

When I run a test on that class, the Lib is undefined. 当我在该类上运行测试时,库未定义。

describe('tests', () => {
  let service: Service;
  beforeEach(() => service = new Service());
  it('test', () => {
    service.stuff();
  });
});

The test itself works. 测试本身有效。 If I test for a hardcoded return value it works, so the overall setups seems to be fine. 如果我测试一个硬编码的返回值,它可以工作,因此整体设置似乎很好。

I use a project skeleton created with angular-cli. 我使用由angular-cli创建的项目骨架。 Some versions: 一些版本:

@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

Im quite new to angular, so i have no clue what happens behind the curtains. 我对棱角还很陌生,所以我不知道幕后会发生什么。 I would expect that all imports just get resolved. 我希望所有进口都得到解决。

Isn't that the case? 不是这样吗

you can only using import Lib from 'lib' if your lib export default the Lib symbol, if not you need using one of formular such as: 如果您的lib export default使用Lib符号,则只能使用import Lib from 'lib' ,如果不需要,则需要使用以下公式之一:

import * as Lib from 'lib'

or 要么

import {Lib} from 'lib'

learn more about import in Mozilla Developer Network 在Mozilla开发人员网络中了解有关导入的更多信息

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

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