错误:意外的值' undefined'由模块“DynamicTestModule”宣布'

时间:2017-07-19 15:21:58

标签: angularjs unit-testing typescript webpack include

在我的Angular / Typescript / Webpack项目中,我在代码修改后重写了单元测试。

运行非常基本的组件测试时,问题的症状是错误:"错误:意外的值' undefined'由模块' DynamicTestModule'"

声明

为了调试这个问题,我结束了从组件的构造函数中删除所有依赖项,基本上是所有代码。该组件什么也没做,但我仍然得到了错误。它不可能是循环依赖,因为没有依赖。

文件夹文件是:

--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)。

2 个答案:

答案 0 :(得分:0)

真实问题VisualCode发现ProfileComponent在./profile.component中,但是console.log行打印出来的那些未定义的&#39;并且没有抛出编译/转换错误。

请注意文件夹中有一个单独的.html文件,但代码没有引用它。

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

我不知道这个特定文件集有什么问题,因为在这个项目中有很多文件夹具有完全相同的命名约定/设置,可以正确运行测试。

我将此处留在此处,以防有人遇到此错误。

答案 1 :(得分:0)

我也遇到了这个问题。我正在测试的组件未在任何模块中声明。我已注释掉模块中的代码,删除注释后,该错误已修复。

相关问题