首页 > 解决方案 > 带有自定义类型的角度单元测试给出了找不到命名空间

问题描述

我正在为我的 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 找不到命名空间 'google'但没有运气

NPM 包

Angular : 8.2.12
jasmine-core : ~3.5.0
karma: ^4.4.1
karma-jasmine : ~2.0.1
typescript: 3.5.3

标签: angularunit-testingangular8

解决方案


查看您的tsconfig.json以及如何配置(最有可能)包含Company.typings.ts. 然后看看你tsconfig.spec.json的包含Company.typings.ts相同的方式。我相信你必须玩typesarray or typerootsor includes

TypeScript 2:无类型 npm 模块的自定义类型


推荐阅读