2017-07-19 84 views
2

在我的Angular/Typescript/Webpack项目中,我在代码修改后重写了单元测试。 “错误:意外的值‘未定义’被模块DynamicTestModule“申报”错误:由模块'DynamicTestModule'声明的未定义值'undefined'

问题的症状运行一个非常基本的组件测试时发生错误

为了调试的问题,我清盘从组件的构造函数和基本上所有代码中剥离出所有依赖项。该组件什么都没做,但我仍然有错误。它不可能是循环依赖,因为没有依赖关系。

文件的文件夹为:

--profile 
----profile.component.ts 
----profile.component.spec.ts 
----profile.component.html 
----index.ts (barrel) 

组件(以便找出问题删除最有意义的代码)是:

import { Component } from '@angular/core'; 
@Component({ 
    selector: 'profile', 
    template: `<h1></h1>` 
}) 
export class ProfileComponent { 
    title = 'Test Tour of Heroes'; 
    constructor(){} 
} 

而且产品的规格有:

import { NO_ERRORS_SCHEMA, DebugElement } from '@angular/core'; 
import { ComponentFixture, TestBed } 
    from '@angular/core/testing'; 

import { ProfileComponent } from './profile.component'; 

console.log(ProfileComponent); 

fdescribe('Component: Jasmine Spy Test',() => { 

    console.log(ProfileComponent); 

    let fixture: ComponentFixture<ProfileComponent>; 
    let component: ProfileComponent; 
    let fixture2: ComponentFixture<ProfileComponent>; 

    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ProfileComponent] 
    }).compileComponents(); 

    fixture = TestBed.createComponent(ProfileComponent); 
    component = fixture.componentInstance; 
    }); 
    it(`should create instance of objects`,() => { 
    expect(1).toBe(1); 
    }); 
}); 

版本:

"@angular/core": "^4.0.0", 
"karma": "~1.4.1" 
"webpack": "^2.3.3" 

还有很多其他软件包,但我不认为这是问题。

我使用VisualCode 1.14.0(1.14.0)。

回答

0

真正的问题VisualCode发现ProfileComponent在./profile.component中,但是打印出来的console.log行表示'undefined',并且没有编译/传输错误。

请注意,文件夹中存在单独的.html文件,但代码不会引用它。

当我重命名profile.component.html文件时,ProfileComponent开始具有值(不再未定义) - 必须是关于webpack构建的东西。

我不知道这个特定的一组文件有什么问题,因为在这个项目中有许多文件夹使用完全相同的命名约定/设置来正确运行测试。

我要离开这里以防万一有人跑过这个错误。

+0

当我在类似的情况下遇到这个错误,它只是帮助退出所有正在运行的Node/npm skripts。 这听起来像是一个Webpack bug ...但我无法重现它。 – jvoigt