2014-03-03 41 views
2

當編譯我的代碼,打字稿包括在每個文件的頂部的__extends聲明:打字稿代碼覆蓋率

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%的代碼覆蓋率。

有沒有人解決過這個問題?

回答

1

自2.1,外接助手庫打字稿的支持,所有的發射功​​能轉到tslib

npm install --save tslib 

改變你的tsconfig:

{ 
    "compilerOptions": { 
     //all the other stuff 
     "importHelpers": true 
    } 
} 

然後打字稿會自動導入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)); 
+0

這似乎是目前正確的解決方案,謝謝 – stolli

-2

打字稿編譯器將在具有extends關鍵字的每個文件的頂部生成此文件。使這個單一用途的唯一方法是編譯成一個帶有--out編譯器標誌的js文件。

+1

感謝您的回答,但這只是對問題的重述。很顯然,編譯爲單個文件會產生幾乎無用的覆蓋報告。似乎TSC應該有一個選擇,或者應該有辦法通過業力覆蓋選項來忽略這些多個實例......或者甚至是一個創造性的shell腳本解決方案?好奇別人可能做了什麼。 – stolli

3

我在typescript codeplex上找到了一個工作項目。我希望打字傢伙很快解決這個問題。你可以在這裏找到更多的:typescript workitem 2002

+0

謝謝,我們實際上提交了一張類似的票據,記錄爲重複。與此同時,我們分叉了編譯器並自己實現了_noExtends選項,它的功能就像一個魅力;) – stolli

+0

CodePlex問題現已關閉。請嘗試:https://github.com/Microsoft/TypeScript/issues/1350 – elwyn

3

我剛剛在我的腳本任務中創建了一個函數,它將一個頭添加到任何使用繼承的文件的頂部。代碼覆蓋率提高了很多。我使用的伊斯坦布爾,所以我的函數如下所示:

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(); 
    }); 
} 

我其實可以發佈爲一飲而盡插件,但我希望有新的方式來儘快解決問題。

1

下面是使用their answer中提供的功能@jedmao的一個示例,其中包含吞嚥任務。 我稍微修改了它以處理var __extends=不是文件中的第一件事(例如,如果您有'use strict'/// <references標記)。您應該也可以使用os.EOL作爲jedmao而不是\n,因爲我在這裏執行此操作。

var gulp  = require('gulp'); 
var through2 = require('through2'); 

gulp.task('my-gulp-task', function() { 
    gulp.src('*.ts') 
     .pipe(myTypeScriptCompiler()) 
     .pipe(istanbulIgnoreTypeScriptExtend()) 
     .pipe(gulp.dest('myDestFolder')); 
}); 

function istanbulIgnoreTypeScriptExtend() { 
    var tsExtends = /var __extends =/; 
    return through2.obj(function(file, enc, done) { 
     if (file.isBuffer() && tsExtends.test(file.contents)) { 
      var rows = file.contents.toString().split('\n'); 
      for (var i = 0; i < rows.length; i++) { 
       if (rows[i].indexOf('var __extends =') === 0) { 
        rows.splice(i, 0, '/* istanbul ignore next: TypeScript extend */'); 
        break; 
       } 
      } 
      file.contents = new Buffer(rows.join('\n')); 
     } 
     this.push(file); 
     done(); 
    }); 
}