我正在尝试为使用第三个JS库的组件设置测试。该库具有一些类型,并且在项目中场景可以顺利进行。当我尝试在Karma中运行标准展台时,会触发错误:
键入名称未定义
我的项目结构(简化)是这样的:
package.json
angular.json
karma.conf.js
all tsconfig files
typings/
*typing_name*.d.ts
app/
- components/
- map/
map.component.ts
...
map.component.spec.ts
...
我添加了以下内容:
ts.config.spec.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "es2015",
"target": "es6",
"outDir": "./out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"app/test.ts",
"app/polyfills.ts"
],
"include": [
"typings/*.d.ts"
]
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"skipLibCheck": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es5",
"typeRoots": [
"typings/*.d.ts",
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
],
"paths": {
"jszip": [
"node_modules/jszip/dist/jszip.min.js"
]
}
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
在map.component.ts
中,我使用如下定义:
import { Something, SomethingElse, AnotherThing, ...} from 'my-custom-typing';
测试非常基本,仅此而已:
describe('NngMapComponent', () => {
let component: NngMapComponent;
let fixture: ComponentFixture<NngMapComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [ToastrService, TranslationService, DataService, ShareService],
imports: [
HttpClientModule,
ToastrModule.forRoot({
preventDuplicates: true
}),
],
declarations: [NngMapComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(NngMapComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should create', () => {
expect(component).toBeTruthy();
});
});
运行测试将导致此错误:
ReferenceError:键入名称未定义
我想念什么?
答案 0 :(得分:0)
我认为您需要在typeRoots
中指定tsconfig.spec.json
:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "es2015",
"target": "es6",
"outDir": "./out-tsc/spec",
"types": [
"jasmine",
"node"
],
"typeRoots": [
"typings/*.d.ts", // this right here
],
},
"files": [
"app/test.ts",
"app/polyfills.ts"
],
"include": [
// "typings/*.d.ts" => Remove this, I don't think it's needed.
]
}
尝试一下。我担心types
和typeRoots
的属性可能会发生冲突,但是您可以修改它们。