Angular 2测试依赖项

时间:2016-11-29 17:02:57

标签: angular karma-runner angular-cli

我正在尝试使用Angular 2测试组件,但是在加载依赖项时遇到了问题。

我在测试UserModule时尝试导入UserShowcaseComponent时出现了Karma错误,如果我不导入它,我会收到很多组件错误。

测试设置是angular-cli生成的,我的具体测试如下

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { UserModule } from '../../user.module';
import { UserShowcaseComponent } from './user-showcase.component';

describe('UserShowcaseComponent', () => {
    let component: UserShowcaseComponent;
    let fixture: ComponentFixture<UserShowcaseComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [UserModule],     <---------------------------------- NOTE
        })
            .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(UserShowcaseComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

来自Karma的错误就是这个(这是完整输出,没有运行测试):

29 11 2016 16:49:40.478:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/
29 11 2016 16:49:40.479:INFO [launcher]: Launching browser Chrome with unlimited concurrency
29 11 2016 16:49:40.649:INFO [launcher]: Starting browser Chrome
29 11 2016 16:49:42.010:INFO [Chrome 53.0.2785 (Linux 0.0.0)]: Connected on socket /#Jpv1IioGnou6CQtUAAAA with id 98117142
Chrome 53.0.2785 (Linux 0.0.0) ERROR

1 个答案:

答案 0 :(得分:0)

问题是Karma没有加载供应商文件,而且没有正确报告。自angular-cli 1.0.0-beta.20起修复此报告问题,请参阅this commit

解决方案是在karma.conf.js中添加供应商库,在我的情况下添加

{ pattern: './node_modules/jquery/dist/jquery.min.js', watched: false }

files

是的,我知道我不应该使用jquery。

相关问题