【问题标题】:Angular unit testing with custom typings gives Cannot find namespace带有自定义类型的角度单元测试给出了找不到命名空间
【发布时间】: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


    【解决方案1】:

    查看您的 tsconfig.json 以及它是如何配置的(很可能)以包含 Company.typings.ts。然后以同样的方式查看您的tsconfig.spec.json 以包含Company.typings.ts。我相信你必须玩types数组或typerootsincludes

    TypeScript 2: custom typings for untyped npm module

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 2017-01-01
      • 2018-04-06
      • 2018-10-25
      • 2018-03-11
      • 2015-09-15
      • 2015-02-02
      • 2012-01-26
      • 2020-05-02
      相关资源
      最近更新 更多