如何使用 nyc 从函数覆盖中排除用 TypeScript 编写的私有构造函数?
How can I exclude private constructors which is written in TypeScript from function coverage using nyc?
我想从 函数覆盖范围中排除 private constructor
。
实际上我更喜欢在 Factory Pattern
.
中使用 private constructor
示例代码
class Instance { }
class InstanceA extends Instance {
public constructor() { super(); }
}
class InstanceB extends Instance {
public constructor() { super(); }
}
class InstanceC extends Instance {
public constructor() { super(); }
}
class Factory {
private constructor() {
/* I want to exclude this constructor */
}
public static createInstance(option: number): Instance {
switch(option) {
case 1:
return new InstanceA();
case 2:
return new InstanceB();
case 3:
return new InstanceC();
default:
return undefined;
}
}
}
class ClientCode {
private name: string;
private age: number;
private myInstance: Instance;
public constructor(name: string, age: number, option: number) {
this.name = name;
this.age = age;
this.myInstance = Factory.createInstance(option);
}
public foo(): void {
/* do something */
}
}
nyc
选项
"nyc": {
"cache": false,
"extension": [
".ts",
".tsx"
],
"include": [
"script/**/*.ts"
],
"exclude": [
"**/*.d.ts",
"script/entries/",
"**/GlobalEnums.ts"
],
"require": [
"ts-node/register/transpile-only",
"source-map-support/register"
],
"reporter": [
"html",
"text",
"text-summary"
],
"temp-dir": "./coverage/.nyc_output"
}
正如我在 comment
中提到的,我想排除 private constructor
。
这使我的覆盖率降低了。
我怎样才能排除这个?
由于 nyc
只是伊斯坦布尔的 CLI 包装器,您应该能够使用它的 special comments 来禁用它:
class Factory {
/* istanbul ignore next */
private constructor() {
/* I want to exclude this constructor */
}
我想从 函数覆盖范围中排除 private constructor
。
实际上我更喜欢在 Factory Pattern
.
private constructor
示例代码
class Instance { }
class InstanceA extends Instance {
public constructor() { super(); }
}
class InstanceB extends Instance {
public constructor() { super(); }
}
class InstanceC extends Instance {
public constructor() { super(); }
}
class Factory {
private constructor() {
/* I want to exclude this constructor */
}
public static createInstance(option: number): Instance {
switch(option) {
case 1:
return new InstanceA();
case 2:
return new InstanceB();
case 3:
return new InstanceC();
default:
return undefined;
}
}
}
class ClientCode {
private name: string;
private age: number;
private myInstance: Instance;
public constructor(name: string, age: number, option: number) {
this.name = name;
this.age = age;
this.myInstance = Factory.createInstance(option);
}
public foo(): void {
/* do something */
}
}
nyc
选项
"nyc": {
"cache": false,
"extension": [
".ts",
".tsx"
],
"include": [
"script/**/*.ts"
],
"exclude": [
"**/*.d.ts",
"script/entries/",
"**/GlobalEnums.ts"
],
"require": [
"ts-node/register/transpile-only",
"source-map-support/register"
],
"reporter": [
"html",
"text",
"text-summary"
],
"temp-dir": "./coverage/.nyc_output"
}
正如我在 comment
中提到的,我想排除 private constructor
。
这使我的覆盖率降低了。
我怎样才能排除这个?
由于 nyc
只是伊斯坦布尔的 CLI 包装器,您应该能够使用它的 special comments 来禁用它:
class Factory {
/* istanbul ignore next */
private constructor() {
/* I want to exclude this constructor */
}