【发布时间】:2020-07-27 02:07:41
【问题描述】:
我正在为我的 Angular 项目使用自定义类型,以避免在客户端进行严格类型的返工。但是,它在运行单元测试时不起作用。
我不确定如何在运行单元测试时引入这个命名空间。下面是我们的代码
组件
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
title = 'app';
public employees: Company.Core.DTO.IEmployee[];
}
规格
import { NO_ERRORS_SCHEMA } from "@angular/core";
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('SearchComponent',
() => {
let fixture: ComponentFixture<AppComponent>;
let component: AppComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
schemas: [NO_ERRORS_SCHEMA],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
TypeLite 自动生成的文件 (app/typings/Company.typings.ts) 中的打字
declare module Company.Core.DTO {
interface IEmployee {
Number: number;
Age: number;
Name: string;
}
}
执行单元测试时出错
错误 TS2503:找不到命名空间公司
这只发生在单元测试编译期间。运行应用程序时,我们没有收到此错误。
试过这个Angular2 Cannot find namespace 'google' 但没有运气
NPM 包
Angular : 8.2.12
jasmine-core : ~3.5.0
karma: ^4.4.1
karma-jasmine : ~2.0.1
typescript: 3.5.3
【问题讨论】:
标签: angular unit-testing angular8