我在用phantomjs进行单元测试时遇到了这个问题。我认为浏览器phantomjs不支持导入继承。以下是问题详情:
环境:
我有这些模型类:
base.model.ts
export class BaseModel {
id?: number;
name?: string;
}
customer.model.ts
import { BaseModel } from './base.model';
export class CustomerModel extends BaseModel {
address?: string;
// more properties defined
}
home.model.ts
import { BaseModel } from './base.model';
import { CustomerModel } from './customer.model';
export class HomeModel extends BaseModel {
param1: CustomerModel;
constructor(data?: any) { // write some logic in here }
}
home.service.ts
import { Injectable } from '@angular/core';
import { Home } from './home.model';
@Injectable()
export class HomeService {
constructor(){ }
get() {
let home = new HomeModel();
}
}
重现行为的步骤
npm run test
将执行karma start config/karma.conf.js
错误
PhantomJS 2.1.1(Windows 8 0.0.0) ERROR TypeError:undefined:不能设置为原型!
根本原因:
经过几天的调查,我发现当单元测试通过new HomeModel()
时,问题才会发生。然后它与BaseModel
与extends
相关,我想当我们new HomeModel()
时,它首先初始化BaseModel
(导入将从上到下),然后{之后初始化{1}}。在CustomerModel
中,它仍然导入CustomerModel
到BaseModel
。
答案 0 :(得分:0)
面临与"typescript": "2.4.0"
相同的问题:
更新了以下依赖项,并且有所帮助:
"@types/core-js": "0.9.42",
"html-loader": "^0.5.1",
"jasmine": "^2.7.0",
"jasmine-core": "^2.7.0",
"karma": "^1.7.0",
"karma-webpack": "^2.0.4",