なっく日報

技術やら生活やらのメモ

istanbul + mocha設定メモ

嵌るポイントをメモ。

下記ページがよくまとまっているので、細かい手順等はこちらを見ていただければと。 http://www.clock.co.uk/blog/npm-module-code-coverage-in-2-simple-stepswww.clock.co.uk

mochaコマンドはそのまま渡せないので、_mochaを使う

参考にした上記のページに書いてましたが、

Mocha users, note the _mocha executable path contains an underscore. This is because the executable without an underscore forks processes to run your tests, but istanbul needs the tests in the same process. Other test framework usage may vary.

だそう。同じプロセスで動かすためには、_mochaから起動しないとダメだそうです。

package.jsonの記述は

"scripts": {
  "test":
    "istanbul test _mocha -- test"
  }
}

みたいになります。 参考ページでは./node_modules/.bin/istanbul で、相対パスで指定していますが、package.jsonの中では、./node_modules/.bin にPATHが通るので、コマンド名のみでOKです。

なお、mochaのオプションはtest/mocha.opts

--require intelli-espower-loader
--ui bdd
--reporter tap
--timeout 5000
--no-colors
--recursive 

みたいに書いておくとよいでしょう(ちなみにpower-assert使うのが最近のデフォです)

istanbul testはdeprecated

istanbul test _mocha -- test のように、istanbul test を使えば、

npm t で通常のテストを実行 npm t --coverageカバレッジが出力できるようになる、やった・・・と思っていたら、deprecatedなよう。 ↓
GitHub - gotwarlost/istanbul: Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.

"scripts": {
  "test:coverage":
    "istanbul cover _mocha -- test"
  }
}

みたいにタスクを増やしたほうがよさそう。

npm tnpm test の短縮系です。