Typescript代码覆盖多个uuu扩展声明

Typescript代码覆盖多个uuu扩展声明,typescript,code-coverage,karma-runner,istanbul,Typescript,Code Coverage,Karma Runner,Istanbul,在编译我的代码时,TypeScript在每个文件的顶部都包含一个扩展声明: var __extends = this.__extends || function (d, b) { /* istanbul ignore next */ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.proto

在编译我的代码时,TypeScript在每个文件的顶部都包含一个扩展声明:

var __extends = this.__extends || function (d, b) {
    /* istanbul ignore next */
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
这在幕后效果很好,但在使用业力覆盖率之类的工具生成报告时会产生不一致。此声明包含两个函数调用和代码中的一个分支(使用),它们只在第一个声明中执行,留下几十个(如果不是几百个)后续声明,没有覆盖范围。这使得一个代码覆盖率为100%的文件看起来在覆盖率报告中被严重覆盖


有人解决了这个问题吗?

typescript编译器将在每个包含
extends
关键字的文件上生成这个问题。唯一可以一次性使用的方法是使用
--out
编译器标志编译成单个js文件。

我在typescript codeplex中找到了一个工作项。我希望typescript的人能尽快解决这个问题。您可以在这里找到更多信息:

我刚刚在脚本任务中创建了一个函数,它在使用继承的任何文件的顶部附加一个头。代码覆盖率大大提高了这一点。我使用的是伊斯坦布尔,所以我的函数如下所示:

function istanbulIgnoreTypeScriptExtend() {
    var tsExtends = /^var __extends =/;
    return through.obj(function(file, enc, done) {
        if (file.isBuffer() && tsExtends.test(file.contents)) {
            file.contents = Buffer.concat([
                new Buffer('/* istanbul ignore next: TypeScript extend */' + os.EOL),
                file.contents
            ]);
        }
        this.push(file);
        done();
    });
}

实际上,我可以将其发布为gulp插件,但我希望能尽快找到解决问题的新方法。

下面是一个使用gulp任务中提供的函数的示例。
我稍微修改了它,以处理
var\uu extends=
不是文件中的第一项内容(例如,如果您有
'use strict'
//,自2.1版以来,外部助手库的typescript支持,所有发出的函数都将进入
tslib

npm install --save tslib
更改您的tsconfig:

{
    "compilerOptions": {
        //all the other stuff
        "importHelpers": true
    }
}
然后,如果需要,TypeScript将自动导入
tslib
包 比如下面的例子

var tslib_1 = require("tslib");

var MyClass = (function (_super) {
    tslib_1.__extends(MyClass, _super);
    function MyClass() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return MyClass;
}(controller_1.Controller));

感谢您的回答,但这只是对问题的重申。显然,编译到单个文件将生成几乎无用的覆盖率报告。TSC似乎应该有一个选项,或者应该有一种方法通过karma覆盖率选项忽略这些多个实例……甚至是一个创造性的shell脚本解决方案?好奇吗其他人可能已经做了什么。谢谢,我们实际上提交了一份类似的通知单,注明为副本。与此同时,我们分叉了编译器并自己实现了_noExtends选项,它像一个符咒一样工作;)CodePlex问题现在已经解决。尝试:这似乎是当前正确的解决方案,谢谢
var tslib_1 = require("tslib");

var MyClass = (function (_super) {
    tslib_1.__extends(MyClass, _super);
    function MyClass() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return MyClass;
}(controller_1.Controller));