2017-07-19 51 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