在 MSBuild 中集成 javascripts 单元测试代码覆盖率

Integrating javascripts unit tests code coverage in MSBuild

I am using Jasmine and Karma for writing unit tests and code coverage. I have created the tasks using Gulp and 运行 them through task runner explorer in VS 2015 update 3.

var gulp = require("gulp");
var Server = require('karma').Server;
var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');

gulp.task('unit-tests', function (done) {
    new Server({
        configFile: __dirname + '/karma.conf.js'
    }, done).start();
});

gulp.task('code-coverage', function () {
    return gulp.src('_reports/coverage-javascript.json')
        .pipe(remapIstanbul({
            reports: {
                'json': '_reports/coverage-typescript.json',
                'html': '_reports/html-report'
            }
        }));
});

I want to read the generated html results file, i.e. from _reports/html-report/index.html file during Gated Builds or Nightly builds. I want to use this code coverage to perform certain actions like stopping the build if code coverage is below 80% or when a test failed.

How can I do that?

我已经实施了一个解决方案,只要任何单元测试失败,MSBuild 就会失败。基本上我在 project.csproj 文件中写了一个自定义目标,它在 'CompileTypeScript' 目标之后 运行s。

<PropertyGroup>
    <CompileDependsOn>
      $(CompileDependsOn);
      GulpBuild;
    </CompileDependsOn>
</PropertyGroup>
<Target Name="GulpBuild" DependsOnTargets="CompileTypeScript">
    <Exec Command="./node_modules/.bin/gulp task-name" />
</Target>

这个任务会运行在Visual studio编译TS到JS之后。在构建服务器中 'Path' 变量没有为 'gulp' 设置,这就是为什么通过 node_modules .bin 文件夹传递命令。