在我的 Angular/Typescript/Webpack 项目中,我在代码修改后重写单元测试。
问题的症状是在运行非常基本的组件测试时出错:“错误:模块‘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)。
最佳答案
真正的问题 VisualCode 识别出 ProfileComponent 在 ./profile.component 中,但是打印出来的 console.log 行说“未定义”并且没有抛出编译/传输错误。
请注意,文件夹中有一个单独的 .html 文件,但代码未引用它。
当我重命名 profile.component.html 文件时,ProfileComponent 开始具有值(value)(不再是未定义的)——一定是关于 webpack 构建的。
我不知道这组特定的文件有什么问题,因为在这个项目中有许多具有完全相同的命名约定/设置的文件夹确实可以正确运行测试。
我把它留在这里以防有人遇到这个错误。
关于angularjs - 错误 : Unexpected value 'undefined' declared by the module 'DynamicTestModule' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45194829/