LoginSignup
9
7

More than 1 year has passed since last update.

gulp+mocha+istanbulでnode.jsのカバレッジを取りつつJenkinsでテスト結果を表示する

Last updated at Posted at 2016-02-03

gulp-mochaとistanbulを使ってローカルでコンソールにテスト結果を表示しつつ、カバレッジも取るといったことはすごく簡単にできたが、JenkinsでJUnit形式で出力されたテスト結果ファイルをパースして結果を表示しようと思ったら困ったのでメモ。

mochaの標準のRepoterでは標準出力にしか出力してくれなくて、ファイルに出せなかった。なので npm install xunit-file を追加して、 gulp-mochaのオプションを mocha({repoter: "xunit-file"}) のように指定することで xunit.xml に出力される。
(gulpで標準出力をそのままファイルに落とす方法無いの??)

gulpfile.js
var gulp = require("gulp"),
    mocha = require("gulp-mocha"),
    istanbul = require('gulp-istanbul');

// テストのカバレッジを取得するための準備
gulp.task('pre-test', function () {
  return gulp.src(['routes/*.js'])
    .pipe(istanbul())
    .pipe(istanbul.hookRequire());
});

// テスト
gulp.task("test", ["pre-test"], function() {
  return gulp.src(["test/*.js"], {read: false})
    .pipe(mocha({reporter: "xunit-file", timeout: "5000"}))
    .pipe(istanbul.writeReports('coverage'))
    .pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } }));
});

istanbulを使って coverage/lcov-report/index.html にカバレッジ結果を出力しているので、これを JenkinsのPublish HTML Reportプラグインを使って表示する
スクリーンショット_020316_100945_AM.jpg

テスト結果をxunit.xmlに出力しているのでJUnitテスト結果の集計で表示する
スクリーンショット_020316_100809_AM.jpg

これでJenkinsでカバレッジの結果とテスト結果の両方が表示できる。

9
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7