typescript - 如何使用 nyc 从函数覆盖范围中排除用 TypeScript 编写的私有构造函数?
问题描述
我想private constructor
从功能覆盖中排除。
其实我更喜欢用private constructor
in Factory Pattern
。
示例代码
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
它只是一个围绕 istanbul 的 CLI 包装器,你应该能够使用它的特殊注释来禁用它:
class Factory {
/* istanbul ignore next */
private constructor() {
/* I want to exclude this constructor */
}
推荐阅读
- python - 即使元素确实存在,我也会在 selenium 中不断收到 NoSuchElementException
- apache-beam - ProfileOptions profile_cpu=True 的数据流作业不写入配置文件
- firebase - 如何在 Android Studio 上使用 Firebase 重新验证 Facebook 登录用户?
- swift - SwiftUI:图像异步更改后视图不更新
- javascript - 反应对象数组的 setState 语法
- azure-maps - 使用 ng-azure-maps 将弹出窗口添加到 azure 地图图层
- flutter - Flutter、Consumer 中的 initState() 和 Provider 出错
(脏,依赖) - python - Python请求从url下载不起作用
- angular - 如何重构角度服务中的某些方法?
- javascript - 如何判断 javascript 字符串是数字还是日期?