[Jenkins]Coffee Script Coverage Karmaでカバレッジを測る

Coffee Script Coverage 計測をする流れは以下。

Karmaセッティング

必要なライブラリ

{
    "name": "sample",
    "version": "0.0.1",
    "dependencies": {
        "coffee-script": "1.7.1",
        "jasmine-node": "1.14.1",
        "grunt": "0.3.15",
        "requirejs": "2.1.11",
        "karma": "0.12.1",
        "karma-coverage": "0.2.1",
        "karma-jasmine": "0.1.5",
        "karma-coffee-preprocessor": "0.2.1",
        "karma-requirejs": "0.2.1",
        "karma-junit-reporter": "0.2.1",
        "karma-phantomjs-launcher": "0.1.2",
        "karma-nunit-reporter": "0.0.0",
        "cake": "0.1.1"
    }
}

Cakefileのセッティング

coffee scriptをjavascriptに変換してKarmaを動かすために準備。
fs = require 'fs'
path = require 'path'
{spawn, exec} = require 'child_process'

coffee = (args) ->
  proc =         spawn './node_modules/.bin/coffee', args
  proc.stderr.on 'data', (buffer) -> console.log buffer.toString()
  proc.on        'exit', (status) -> process.exit(1) if status != 0

jasmine = (args) ->
  proc =         spawn './node_modules/.bin/jasmine-node', args
  proc.stdout.on 'data', (buffer) -> console.log buffer.toString()
  proc.on        'exit', (status) -> process.exit(1) if status != 0

task 'buildspec', "build source files in './spec' to './lib'", ->
  target = [
    'src/sample1.coffee'
    'src/sample2.coffee'
    'spec/sample1_spec.coffee'
    'spec/sample2_spec.coffee'
  ]
  coffee ['-j','tmpspec','-o','lib','-c'].concat target
  exec 'sleep 1 && cat lib/tmpspec.js | grep -v "exports\." | grep -v "require(" > sample_spec.js', (err) ->
    throw err if err

Karmaのセッティング

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      'sample_spec.js'
    ],
    browsers: ['PhantomJS'],
    singleRun: true,
    reporters: ['progress', 'coverage'],
    preprocessors: { 'sample_spec.js': ['coverage']},
    coverageReporter: {
      type: 'cobertura',
      dir: 'coverage/'
    }
  });
};

Karmaの実行

# Make JS for test
./node_modules/cake/bin/cake buildspec
# Execute Karma
./node_modules/karma/bin/karma start karma.conf.js
# Arrange coverage report for Jenkins
cp "coverage/PhantomJS 1.9.7 (Linux)/cobertura-coverage.xml" coverage/

Jenkins 設定

zuqqhi2

某Web系の会社でエンジニアをやっています。 学術的なことに非常に興味があります。 趣味は楽器演奏、ジョギング、読書、料理などなど手広くやっています。